Submission #6919421
Source Code Expand
// #pragma GCC target("avx2") // CPU 処理並列化 // #pragma GCC optimize("O3") // CPU 処理並列化 // #pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす #include<stdio.h> #include<math.h> #include<algorithm> #include<queue> #include<deque> #include<stack> #include<string> #include<string.h> #include<vector> #include<set> #include<map> #include<bitset> #include<stdlib.h> #include<cassert> #include<time.h> #include<bitset> #include<numeric> using namespace std; const long long mod=1000000007; const long long inf=mod*mod; const long long d2=(mod+1)/2; const double EPS=1e-10; const double INF=1e+10; const double PI=acos(-1.0); const int C_SIZE = 3121000; long long fact[C_SIZE]; long long finv[C_SIZE]; long long inv[C_SIZE]; long long Comb(int a,int b){ if(a<b||b<0)return 0; return fact[a]*finv[b]%mod*finv[a-b]%mod; } void init_C(int n){ fact[0]=finv[0]=inv[1]=1; for(int i=2;i<n;i++){ inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod; } for(int i=1;i<n;i++){ fact[i]=fact[i-1]*i%mod; finv[i]=finv[i-1]*inv[i]%mod; } } long long pw(long long a,long long b){ if(a<0LL)return 0; if(b<0LL)return 0; long long ret=1; while(b){ if(b%2)ret=ret*a%mod; a=a*a%mod; b/=2; } return ret; } int ABS(int a){return max(a,-a);} long long ABS(long long a){return max(a,-a);} double ABS(double a){return max(a,-a);} int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; } // ここから編集しろ vector<int>g[110000]; char in[110000]; int need[110000]; int dfs(int a,int b){ int ret=0; if(in[a]=='W')ret=1; for(int i=0;i<g[a].size();i++){ if(b==g[a][i])continue; ret|=dfs(g[a][i],a); } need[a]=ret; return ret; } int bes=0; int dp[110000]; int dp2[110000]; int cost[110000]; void calc(int a,int b){ int ji=0; pair<int,int> b1=make_pair(-10,-10); pair<int,int> b2=make_pair(-10,-10); pair<int,int> d1=make_pair(-10,-10); pair<int,int> d2=make_pair(-10,-10); //printf("%d %d\n",a,b); for(int i=0;i<g[a].size();i++){ if(need[g[a][i]]){ ji++; if(g[a][i]!=b){ calc(g[a][i],a); cost[a]+=2; cost[a]+=cost[g[a][i]]; if(dp[g[a][i]]>b1.first){ b2=b1;b1=make_pair(dp[g[a][i]],i); }else if(dp[g[a][i]]>b2.first){ b2=make_pair(dp[g[a][i]],i); } if(dp2[g[a][i]]>d1.first){ d2=d1;d1=make_pair(dp2[g[a][i]],i); }else if(dp2[g[a][i]]>d2.first){ d2=make_pair(dp2[g[a][i]],i); } } } } if(ji%2==0&&in[a]=='W')cost[a]++; if(ji%2&&in[a]=='B')cost[a]++; int f=0; if((ji%2==0&&in[a]=='W')||(ji%2&&in[a]=='B'))f++; else f--; if(b1.second!=d1.second){ bes=max(bes,b1.first+d1.first+f); }else{ bes=max(bes,b1.first+d2.first+f); bes=max(bes,b2.first+d1.first+f); } bes=max(bes,d1.first+f); bes=max(bes,b1.first); int c1=b1.first+1; if((ji%2==0&&in[a]=='W')||(ji%2&&in[a]=='B'))c1++; else c1--; int c2=1; if((ji%2==0&&in[a]=='W')||(ji%2&&in[a]=='B'))c2++; else c2--; dp[a]=max(c2,c1); int e1=d1.first+1; if((ji%2==0&&in[a]=='W')||(ji%2&&in[a]=='B'))e1++; else e1--; int e2=1; dp2[a]=max(e2,e1); } int main(){ int a;scanf("%d",&a); for(int i=0;i<a-1;i++){ int p,q;scanf("%d%d",&p,&q); p--;q--; g[p].push_back(q); g[q].push_back(p); } scanf("%s",in); for(int i=0;i<a;i++){ if(in[i]=='W'){ dfs(i,-1); break; } } int pr=-1; for(int i=0;i<a;i++){ if(need[i]){ pr=i;break; } } if(pr==-1){ printf("0\n");return 0; } calc(pr,-1); int ret=cost[pr]-bes; // printf("%d %d\n",cost[pr],bes); printf("%d\n",ret); }
Submission Info
Submission Time | |
---|---|
Task | F - Monochrome Cat |
User | tozangezan |
Language | C++14 (GCC 5.4.1) |
Score | 800 |
Code Size | 3671 Byte |
Status | AC |
Exec Time | 59 ms |
Memory | 19072 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:141:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] int a;scanf("%d",&a); ^ ./Main.cpp:143:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] int p,q;scanf("%d%d",&p,&q); ^ ./Main.cpp:148:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%s",in); ^
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 800 / 800 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt |
All | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 1_033.txt, 1_034.txt, 1_035.txt, 1_036.txt, 1_037.txt, 1_038.txt, 1_039.txt, 1_040.txt, 1_041.txt, 1_042.txt, 1_043.txt, 1_044.txt, 1_045.txt, 1_046.txt, 1_047.txt, 1_048.txt, 1_049.txt, 1_050.txt, 1_051.txt, 1_052.txt, 1_053.txt, 1_054.txt, 1_055.txt, 1_056.txt, 1_057.txt, 1_058.txt, 1_059.txt, 1_060.txt, 1_061.txt, 1_062.txt, 1_063.txt, 1_064.txt, 1_065.txt, 1_066.txt, 1_067.txt, 1_068.txt, 1_069.txt, 1_070.txt, 1_071.txt, 1_072.txt, 1_073.txt, 1_074.txt, 1_075.txt, 1_076.txt, 1_077.txt, 1_078.txt, 1_079.txt, 1_080.txt, 1_081.txt, 1_082.txt, 1_083.txt, 1_084.txt, 1_085.txt, 1_086.txt, 1_087.txt, 1_088.txt, 1_089.txt, 1_090.txt, 1_091.txt, 1_092.txt, 1_093.txt, 1_094.txt, 1_095.txt, 1_096.txt, 1_097.txt, 1_098.txt, 1_099.txt, 1_100.txt, 1_101.txt, 1_102.txt, 1_103.txt, 1_104.txt, 1_105.txt, 1_106.txt, 1_107.txt, 1_108.txt, 1_109.txt, 1_110.txt, 1_111.txt, 1_112.txt, 1_113.txt, 1_114.txt, 1_115.txt, 1_116.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_000.txt | AC | 2 ms | 4608 KB |
0_001.txt | AC | 2 ms | 4608 KB |
0_002.txt | AC | 2 ms | 4608 KB |
0_003.txt | AC | 2 ms | 4608 KB |
1_003.txt | AC | 2 ms | 4608 KB |
1_004.txt | AC | 2 ms | 4608 KB |
1_005.txt | AC | 2 ms | 4608 KB |
1_006.txt | AC | 2 ms | 4608 KB |
1_007.txt | AC | 2 ms | 4608 KB |
1_008.txt | AC | 2 ms | 4608 KB |
1_009.txt | AC | 2 ms | 4608 KB |
1_010.txt | AC | 2 ms | 4608 KB |
1_011.txt | AC | 2 ms | 4608 KB |
1_012.txt | AC | 2 ms | 4608 KB |
1_013.txt | AC | 2 ms | 4608 KB |
1_014.txt | AC | 2 ms | 4608 KB |
1_015.txt | AC | 2 ms | 4608 KB |
1_016.txt | AC | 2 ms | 4608 KB |
1_017.txt | AC | 2 ms | 4608 KB |
1_018.txt | AC | 2 ms | 4608 KB |
1_019.txt | AC | 2 ms | 4608 KB |
1_020.txt | AC | 2 ms | 4608 KB |
1_021.txt | AC | 2 ms | 4608 KB |
1_022.txt | AC | 2 ms | 4608 KB |
1_023.txt | AC | 2 ms | 4608 KB |
1_024.txt | AC | 2 ms | 4608 KB |
1_025.txt | AC | 2 ms | 4608 KB |
1_026.txt | AC | 2 ms | 4608 KB |
1_027.txt | AC | 2 ms | 4608 KB |
1_028.txt | AC | 2 ms | 4608 KB |
1_029.txt | AC | 2 ms | 4608 KB |
1_030.txt | AC | 2 ms | 4608 KB |
1_031.txt | AC | 2 ms | 4608 KB |
1_032.txt | AC | 2 ms | 4608 KB |
1_033.txt | AC | 2 ms | 4608 KB |
1_034.txt | AC | 2 ms | 4608 KB |
1_035.txt | AC | 2 ms | 4608 KB |
1_036.txt | AC | 2 ms | 4608 KB |
1_037.txt | AC | 2 ms | 4608 KB |
1_038.txt | AC | 2 ms | 4608 KB |
1_039.txt | AC | 2 ms | 4608 KB |
1_040.txt | AC | 2 ms | 4608 KB |
1_041.txt | AC | 2 ms | 4608 KB |
1_042.txt | AC | 2 ms | 4608 KB |
1_043.txt | AC | 2 ms | 4608 KB |
1_044.txt | AC | 2 ms | 4608 KB |
1_045.txt | AC | 19 ms | 9216 KB |
1_046.txt | AC | 23 ms | 11264 KB |
1_047.txt | AC | 32 ms | 7680 KB |
1_048.txt | AC | 31 ms | 11520 KB |
1_049.txt | AC | 37 ms | 13440 KB |
1_050.txt | AC | 32 ms | 12800 KB |
1_051.txt | AC | 25 ms | 8184 KB |
1_052.txt | AC | 7 ms | 5504 KB |
1_053.txt | AC | 24 ms | 7672 KB |
1_054.txt | AC | 11 ms | 5888 KB |
1_055.txt | AC | 24 ms | 8184 KB |
1_056.txt | AC | 14 ms | 6396 KB |
1_057.txt | AC | 3 ms | 4736 KB |
1_058.txt | AC | 22 ms | 8320 KB |
1_059.txt | AC | 13 ms | 5632 KB |
1_060.txt | AC | 31 ms | 9472 KB |
1_061.txt | AC | 39 ms | 11520 KB |
1_062.txt | AC | 21 ms | 8704 KB |
1_063.txt | AC | 24 ms | 7552 KB |
1_064.txt | AC | 17 ms | 6656 KB |
1_065.txt | AC | 4 ms | 4736 KB |
1_066.txt | AC | 28 ms | 7676 KB |
1_067.txt | AC | 9 ms | 5504 KB |
1_068.txt | AC | 20 ms | 7168 KB |
1_069.txt | AC | 13 ms | 5760 KB |
1_070.txt | AC | 21 ms | 6784 KB |
1_071.txt | AC | 28 ms | 7296 KB |
1_072.txt | AC | 20 ms | 6528 KB |
1_073.txt | AC | 6 ms | 4992 KB |
1_074.txt | AC | 4 ms | 4864 KB |
1_075.txt | AC | 20 ms | 6656 KB |
1_076.txt | AC | 43 ms | 9344 KB |
1_077.txt | AC | 27 ms | 7168 KB |
1_078.txt | AC | 12 ms | 5632 KB |
1_079.txt | AC | 20 ms | 6656 KB |
1_080.txt | AC | 21 ms | 6912 KB |
1_081.txt | AC | 59 ms | 15232 KB |
1_082.txt | AC | 52 ms | 16128 KB |
1_083.txt | AC | 33 ms | 7808 KB |
1_084.txt | AC | 50 ms | 14592 KB |
1_085.txt | AC | 53 ms | 18560 KB |
1_086.txt | AC | 55 ms | 19072 KB |
1_087.txt | AC | 35 ms | 9336 KB |
1_088.txt | AC | 31 ms | 9336 KB |
1_089.txt | AC | 28 ms | 8184 KB |
1_090.txt | AC | 29 ms | 8568 KB |
1_091.txt | AC | 32 ms | 9336 KB |
1_092.txt | AC | 27 ms | 8184 KB |
1_093.txt | AC | 52 ms | 12544 KB |
1_094.txt | AC | 49 ms | 14720 KB |
1_095.txt | AC | 34 ms | 7808 KB |
1_096.txt | AC | 41 ms | 10368 KB |
1_097.txt | AC | 49 ms | 13440 KB |
1_098.txt | AC | 47 ms | 14080 KB |
1_099.txt | AC | 38 ms | 9596 KB |
1_100.txt | AC | 38 ms | 9596 KB |
1_101.txt | AC | 35 ms | 7932 KB |
1_102.txt | AC | 34 ms | 8316 KB |
1_103.txt | AC | 39 ms | 9596 KB |
1_104.txt | AC | 37 ms | 9596 KB |
1_105.txt | AC | 44 ms | 9472 KB |
1_106.txt | AC | 45 ms | 9472 KB |
1_107.txt | AC | 34 ms | 7808 KB |
1_108.txt | AC | 38 ms | 8192 KB |
1_109.txt | AC | 45 ms | 9472 KB |
1_110.txt | AC | 44 ms | 9472 KB |
1_111.txt | AC | 44 ms | 9472 KB |
1_112.txt | AC | 44 ms | 9472 KB |
1_113.txt | AC | 34 ms | 7808 KB |
1_114.txt | AC | 38 ms | 8192 KB |
1_115.txt | AC | 44 ms | 9472 KB |
1_116.txt | AC | 43 ms | 9472 KB |