Submission #2503413
Source Code Expand
#include <algorithm> #include <iostream> #include <sstream> #include <string> #include <cstring> #include <vector> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <list> #include <cassert> #include <ctime> #include <climits> using namespace std; #define PB push_back #define MP make_pair #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FORE(i,a,b) for(int i=(a);i<=(b);++i) #define REPE(i,n) FORE(i,0,n) #define FORSZ(i,a,v) FOR(i,a,SZ(v)) #define REPSZ(i,v) REP(i,SZ(v)) typedef long long ll; typedef unsigned long long ull; ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); } const int MAXN=100000; int n; vector<int> adj[MAXN]; char col[MAXN]; bool alive[MAXN]; int deg[MAXN]; int q[MAXN],qhead,qtail; bool gain[MAXN]; pair<ll,ll> dfspath(int at,int par) { // returns (longest path starting here,longest path in subtree) ll mxsofar=gain[at]?n:0; pair<ll,ll> ret=MP(mxsofar,mxsofar); REPSZ(i,adj[at]) { int to=adj[at][i]; if(to==par||!alive[to]) continue; pair<ll,ll> cur=dfspath(to,at); ll curpath=cur.first+1+(gain[at]?n:0); ret.first=max(ret.first,curpath); ret.second=max(ret.second,cur.second); ret.second=max(ret.second,mxsofar+cur.first+1); mxsofar=max(mxsofar,curpath); } return ret; } int solve() { bool anywhite=false; REP(i,n) if(col[i]=='W') anywhite=true; if(!anywhite) return 0; REP(i,n) alive[i]=true,deg[i]=SZ(adj[i]); qhead=qtail=0; REP(i,n) if(deg[i]==1&&col[i]=='B') alive[i]=false,q[qhead++]=i; while(qtail<qhead) { int at=q[qtail++]; REPSZ(i,adj[at]) { int to=adj[at][i]; if(!alive[to]) continue; if(--deg[to]<=1&&col[to]=='B') alive[to]=false,q[qhead++]=to; } } REP(i,n) if(alive[i]&°[i]<=1) assert(col[i]=='W'); REP(i,n) gain[i]=alive[i]&&(col[i]=='B'&°[i]%2==1||col[i]=='W'&°[i]%2==0); int numalive=0; REP(i,n) if(alive[i]) ++numalive; if(numalive==1) return 1; //printf("alive:"); REP(i,n) if(alive[i]) printf(" %d",i+1); puts(""); //printf("gain:"); REP(i,n) if(gain[i]) printf(" %d",i+1); puts(""); int root=-1; REP(i,n) if(alive[i]) { root=i; break; } assert(root!=-1); pair<ll,ll> longpath=dfspath(root,-1); int longedges=longpath.second%n,longgain=longpath.second/n; int degsum=0; REP(i,n) if(alive[i]) degsum+=deg[i]; int gaincnt=0; REP(i,n) if(gain[i]) ++gaincnt; //printf("degsum=%d gaincnt=%d longedges=%d longgain=%d\n",degsum,gaincnt,longedges,longgain); return degsum+gaincnt-longedges-longgain+(longedges-1-longgain)+1; } void run() { scanf("%d",&n); REP(i,n-1) { int a,b; scanf("%d%d",&a,&b); --a,--b; adj[a].PB(b); adj[b].PB(a); } REP(i,n) scanf(" %c",&col[i]); printf("%d\n",solve()); } int main() { run(); return 0; }
Submission Info
Submission Time | |
---|---|
Task | F - Monochrome Cat |
User | krijgertje |
Language | C++14 (GCC 5.4.1) |
Score | 800 |
Code Size | 2915 Byte |
Status | AC |
Exec Time | 51 ms |
Memory | 13312 KB |
Compile Error
./Main.cpp: In function ‘void run()’: ./Main.cpp:88:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d",&n); REP(i,n-1) { int a,b; scanf("%d%d",&a,&b); --a,--b; adj[a].PB(b); adj[b].PB(a); } REP(i,n) scanf(" %c",&col[i]); ^ ./Main.cpp:88:59: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d",&n); REP(i,n-1) { int a,b; scanf("%d%d",&a,&b); --a,--b; adj[a].PB(b); adj[b].PB(a); } REP(i,n) scanf(" %c",&col[i]); ^ ./Main.cpp:88:129: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d",&n); REP(i,n-1) { int a,b; scanf("%d%d",&a,&b); --a,--b; adj[a].PB(b); adj[b].PB(a); } REP(i,n) scanf(" %c",&col[i]); ...
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 | 2560 KB |
0_001.txt | AC | 2 ms | 2560 KB |
0_002.txt | AC | 2 ms | 2560 KB |
0_003.txt | AC | 2 ms | 2560 KB |
1_003.txt | AC | 2 ms | 2560 KB |
1_004.txt | AC | 2 ms | 2560 KB |
1_005.txt | AC | 2 ms | 2560 KB |
1_006.txt | AC | 2 ms | 2560 KB |
1_007.txt | AC | 2 ms | 2560 KB |
1_008.txt | AC | 2 ms | 2560 KB |
1_009.txt | AC | 2 ms | 2560 KB |
1_010.txt | AC | 2 ms | 2560 KB |
1_011.txt | AC | 2 ms | 2560 KB |
1_012.txt | AC | 2 ms | 2560 KB |
1_013.txt | AC | 2 ms | 2560 KB |
1_014.txt | AC | 2 ms | 2560 KB |
1_015.txt | AC | 2 ms | 2560 KB |
1_016.txt | AC | 2 ms | 2560 KB |
1_017.txt | AC | 2 ms | 2560 KB |
1_018.txt | AC | 2 ms | 2560 KB |
1_019.txt | AC | 2 ms | 2560 KB |
1_020.txt | AC | 2 ms | 2560 KB |
1_021.txt | AC | 2 ms | 2560 KB |
1_022.txt | AC | 2 ms | 2560 KB |
1_023.txt | AC | 2 ms | 2560 KB |
1_024.txt | AC | 2 ms | 2560 KB |
1_025.txt | AC | 2 ms | 2560 KB |
1_026.txt | AC | 2 ms | 2560 KB |
1_027.txt | AC | 2 ms | 2560 KB |
1_028.txt | AC | 2 ms | 2560 KB |
1_029.txt | AC | 2 ms | 2560 KB |
1_030.txt | AC | 2 ms | 2560 KB |
1_031.txt | AC | 2 ms | 2560 KB |
1_032.txt | AC | 2 ms | 2560 KB |
1_033.txt | AC | 2 ms | 2560 KB |
1_034.txt | AC | 2 ms | 2560 KB |
1_035.txt | AC | 2 ms | 2560 KB |
1_036.txt | AC | 2 ms | 2560 KB |
1_037.txt | AC | 2 ms | 2560 KB |
1_038.txt | AC | 2 ms | 2560 KB |
1_039.txt | AC | 2 ms | 2560 KB |
1_040.txt | AC | 2 ms | 2560 KB |
1_041.txt | AC | 2 ms | 2560 KB |
1_042.txt | AC | 2 ms | 2560 KB |
1_043.txt | AC | 2 ms | 2560 KB |
1_044.txt | AC | 2 ms | 2560 KB |
1_045.txt | AC | 18 ms | 6016 KB |
1_046.txt | AC | 22 ms | 7552 KB |
1_047.txt | AC | 38 ms | 5760 KB |
1_048.txt | AC | 34 ms | 5760 KB |
1_049.txt | AC | 34 ms | 8960 KB |
1_050.txt | AC | 31 ms | 8704 KB |
1_051.txt | AC | 26 ms | 5752 KB |
1_052.txt | AC | 8 ms | 3456 KB |
1_053.txt | AC | 27 ms | 5624 KB |
1_054.txt | AC | 13 ms | 4220 KB |
1_055.txt | AC | 26 ms | 5752 KB |
1_056.txt | AC | 16 ms | 4732 KB |
1_057.txt | AC | 3 ms | 2688 KB |
1_058.txt | AC | 23 ms | 5504 KB |
1_059.txt | AC | 15 ms | 3712 KB |
1_060.txt | AC | 34 ms | 5760 KB |
1_061.txt | AC | 40 ms | 7936 KB |
1_062.txt | AC | 22 ms | 5888 KB |
1_063.txt | AC | 26 ms | 4992 KB |
1_064.txt | AC | 19 ms | 4352 KB |
1_065.txt | AC | 4 ms | 2816 KB |
1_066.txt | AC | 33 ms | 6140 KB |
1_067.txt | AC | 9 ms | 3328 KB |
1_068.txt | AC | 22 ms | 4760 KB |
1_069.txt | AC | 13 ms | 3584 KB |
1_070.txt | AC | 22 ms | 4480 KB |
1_071.txt | AC | 33 ms | 5376 KB |
1_072.txt | AC | 23 ms | 4736 KB |
1_073.txt | AC | 6 ms | 2944 KB |
1_074.txt | AC | 4 ms | 2816 KB |
1_075.txt | AC | 20 ms | 4224 KB |
1_076.txt | AC | 46 ms | 6528 KB |
1_077.txt | AC | 32 ms | 5248 KB |
1_078.txt | AC | 14 ms | 3840 KB |
1_079.txt | AC | 20 ms | 4224 KB |
1_080.txt | AC | 23 ms | 4480 KB |
1_081.txt | AC | 49 ms | 10624 KB |
1_082.txt | AC | 49 ms | 10880 KB |
1_083.txt | AC | 39 ms | 5760 KB |
1_084.txt | AC | 45 ms | 6784 KB |
1_085.txt | AC | 51 ms | 13056 KB |
1_086.txt | AC | 50 ms | 13312 KB |
1_087.txt | AC | 35 ms | 6776 KB |
1_088.txt | AC | 37 ms | 7032 KB |
1_089.txt | AC | 32 ms | 6264 KB |
1_090.txt | AC | 34 ms | 7160 KB |
1_091.txt | AC | 36 ms | 6776 KB |
1_092.txt | AC | 34 ms | 6264 KB |
1_093.txt | AC | 49 ms | 8704 KB |
1_094.txt | AC | 51 ms | 10240 KB |
1_095.txt | AC | 39 ms | 5888 KB |
1_096.txt | AC | 45 ms | 6784 KB |
1_097.txt | AC | 49 ms | 9344 KB |
1_098.txt | AC | 50 ms | 9856 KB |
1_099.txt | AC | 42 ms | 6652 KB |
1_100.txt | AC | 44 ms | 6780 KB |
1_101.txt | AC | 36 ms | 6012 KB |
1_102.txt | AC | 42 ms | 7036 KB |
1_103.txt | AC | 42 ms | 6652 KB |
1_104.txt | AC | 43 ms | 6780 KB |
1_105.txt | AC | 46 ms | 6528 KB |
1_106.txt | AC | 48 ms | 6528 KB |
1_107.txt | AC | 39 ms | 5888 KB |
1_108.txt | AC | 45 ms | 6784 KB |
1_109.txt | AC | 47 ms | 6400 KB |
1_110.txt | AC | 48 ms | 6656 KB |
1_111.txt | AC | 47 ms | 6400 KB |
1_112.txt | AC | 48 ms | 6528 KB |
1_113.txt | AC | 39 ms | 5888 KB |
1_114.txt | AC | 46 ms | 6784 KB |
1_115.txt | AC | 47 ms | 6400 KB |
1_116.txt | AC | 48 ms | 6656 KB |