Codeforces Round #338 (Div. 2)
A:https://codeforces.com/contest/615/problem/A
题意:
水题,用set存就行了
题解:
set存
代码:
#include <bits/stdc++.h> using namespace std; #define ll long long set<int> s; int main() { int n,m,x,y; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%d",&x); for(int j=1;j<=x;j++){ scanf("%d",&y); s.insert(y); } } if(s.size() == m){ printf("YES\n"); }else{ printf("NO\n"); } return 0; }
B:https://codeforces.com/contest/615/problem/B
题意:
给出一个无向图,每个点都有权值,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链的beauty值 = P*degree[u]。问你所有链中最大的beauty值
题解:
从1到n遍历每个节点对应的最长的子链的长度,更新最大值
代码:
#include <bits/stdc++.h> using namespace std; #define ll long long vector<int> v[100010]; ll dp[100010]; ll max(ll x,ll y) { return x>y ? x:y; } int main() { int n,m,y,x; scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } dp[1] = 1ll*1; ll ans = 1ll*dp[1]*v[1].size(); for(int i=2;i<=n;i++){ dp[i] = 1; for(int j=0;j<v[i].size();j++){ int k = v[i][j]; if(k > i) continue ; dp[i] = max(dp[i],dp[k] + 1); } ans = max(ans , 1ll*dp[i]*v[i].size()); } cout<<ans<<endl; return 0; }
C:https://codeforces.com/contest/615/problem/C
题意:
给出俩个字符串s1和s2,问你能否用s1的子序列构成s2,s1的子序列可以旋转,如果有,输出最少的子序列个数
题解:
暴力求解,从每一个节点往前往后遍历
代码:
#include <bits/stdc++.h> using namespace std; #define ll long long const ll mod = 1e9+7; const int maxx = 3010; char s1[maxx],s2[maxx]; struct node{ int l,r; }; node ans[maxx]; int main() { scanf("%s%s",s1,s2); int l1 = strlen(s1),l2 = strlen(s2); int cnt = 0 ,flag = 0; for(int i=0;i<l2;i++){ int len = 0,l = 0 ,r = 0 ; for(int j=0;j<l1;j++){ if(s2[i] == s1[j]){ int len1 = j,k; for(k=1;k<=j;k++){ if(s2[i+k] != s1[j-k]) break; len1--; } if(k > len){ len = k, l = j ,r = len1; } len1 = j; for(k=1;k<l1 - j;k++){ if(s2[i+k] != s1[j+k]) break; len1++; } if(k > len){ len = k,l = j ,r = len1; } } } if(!len){ flag = 1; break; } i += len-1; ans[cnt].l = l,ans[cnt++].r = r; } if(flag){ printf("-1\n"); return 0; } printf("%d\n",cnt); for(int i=0;i<cnt;i++){ printf("%d %d\n",ans[i].l+1,ans[i].r+1); } return 0; }
D:https://codeforces.com/contest/615/problem/D
代码:
#include <bits/stdc++.h> using namespace std; #define ll long long ll pow_mod(ll a,ll b,ll mod) { ll ans = 1; while(b){ if(b&1) ans = ans*a%mod; a = a*a%mod; b>>=1; } return ans; } const ll mod = 1e9+7; const ll mod1 = 2*mod - 2; const int maxx = 200010; ll a[maxx],b[maxx],flag[maxx]; int main() { ll m,x; int cnt = 0; scanf("%lld",&m); while(m--) { scanf("%lld",&x); if(b[x]){ b[x]++; }else{ flag[++cnt] = x; b[x] = 1; } } ll sum = 1,ans = 1; for(int i=1;i<=cnt;i++){ sum *= (1+b[flag[i]]); sum %= mod1; } for(int i=1;i<=cnt;i++){ ans *= pow_mod(flag[i] , b[flag[i]]*sum/2 , mod); ans %= mod; } printf("%lld\n",ans); return 0; }