Submission #1045131


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); }

// for a given operation (x,d) that does not paint all vertices, determine the canonical of the operation as follows:
// * if there are at least two adjacent vertices of which not every descendant is painted, stop.
// * otherwise there is exactly one adjacent vertex of which not every descendant is painted, name it y.
//   - if operation (y,d-1) would paint the same vertices, go there
//   - otherwise, stop.
// -> the canonical operation paints the same vertices
// -> an operation is canonical iff:
// * it is adjacent to at least two not-fully painted subtrees AND (snuke likes the vertex OR a vertex in a fully painted adjacent subtree)
// * it is adjacent to exactly one not-fully painted subtree AND the other adjacent subtrees are (nearly) exactly fully painted AND (snuke likes the vertex OR a vertex in one of those fully painted subtrees)

const int MAXN=200000;
const int MAXM=MAXN-1;

int n;
int ghead[MAXN],gnxt[2*MAXM],gto[2*MAXM];
char slike[MAXN+1];

int par[MAXN];
int edepth[2*MAXM]; bool elike[2*MAXM];
int dmx[MAXN],dmxidx[MAXN],dmx2[MAXN]; int clike[MAXN],clike2[MAXN];

pair<int,bool> dfs1(int at) {
	pair<int,bool> ret=MP(0,slike[at]=='1'); dmx[at]=dmx2[at]=0,dmxidx[at]=-1,clike[at]=clike2[at]=-1;
	for(int x=ghead[at];x!=-1;x=gnxt[x]) {
		int to=gto[x];
		if(to==par[at]) continue;
		par[to]=at;
		pair<int,bool> cur=dfs1(to);
		edepth[x]=cur.first+1; elike[x]=cur.second;
		if(edepth[x]>ret.first) ret.first=edepth[x]; if(cur.second) ret.second=true;
		if(edepth[x]>dmx[at]) dmx2[at]=dmx[at],dmx[at]=edepth[x],dmxidx[at]=to; else if(edepth[x]>dmx2[at]) dmx2[at]=edepth[x];
		if(elike[x]) if(clike[at]==-1) clike[at]=to; else if(clike2[at]==-1) clike2[at]=to;
	}
	return ret;
}
void dfs2(int at) {
	for(int x=ghead[at];x!=-1;x=gnxt[x]) {
		int to=gto[x];
		if(to!=par[at]) continue;
		edepth[x]=(dmxidx[to]!=at?dmx[to]:dmx2[to])+1;
		elike[x]=slike[to]=='1'||clike[to]!=-1&&clike[to]!=at||clike2[to]!=-1;
		if(edepth[x]>dmx[at]) dmx2[at]=dmx[at],dmx[at]=edepth[x],dmxidx[at]=to; else if(edepth[x]>dmx2[at]) dmx2[at]=edepth[x];
		if(elike[x]) if(clike[at]==-1) clike[at]=to; else if(clike2[at]==-1) clike2[at]=to;
	}
	for(int x=ghead[at];x!=-1;x=gnxt[x]) {
		int to=gto[x];
		if(to==par[at]) continue;
		dfs2(to);
	}
}

void run() {
	scanf("%d",&n);
	REP(i,n) ghead[i]=-1;
	REP(i,n-1) {
		int a,b; scanf("%d%d",&a,&b); --a,--b;
		gnxt[2*i+0]=ghead[a]; ghead[a]=2*i+0; gto[2*i+0]=b;
		gnxt[2*i+1]=ghead[b]; ghead[b]=2*i+1; gto[2*i+1]=a;
	}
	scanf("%s",slike);

	par[0]=-1; dfs1(0); dfs2(0);
	//REP(i,n) printf("%d: [dmx=%d,dmxidx=%d,dmx2=%d][clike=%d,clike2=%d]\n",i,dmx[i],dmxidx[i],dmx2[i],clike[i],clike2[i]);
	//REP(x,2*(n-1)) printf("%d->%d: depth=%d like=%c\n",gto[x^1],gto[x],edepth[x],elike[x]?'V':'-');

	ll ret=1;
	REP(at,n) {
		int mnlike=INT_MAX; int mx=0,mx2=0;
		if(slike[at]=='1') mnlike=0;
		for(int x=ghead[at];x!=-1;x=gnxt[x]) {
			if(elike[x]&&edepth[x]<mnlike) mnlike=edepth[x];
			if(edepth[x]>mx) mx2=mx,mx=edepth[x]; else if(edepth[x]>mx2) mx2=edepth[x];
		}
		ll a=0,b=0;
		if(mnlike<=mx2-1) a+=mx2-1-mnlike+1;

		if(mnlike<=mx2&&mx2<=mx-1) b+=min(mx2+1,mx-1)-mx2+1; // mx2..min(mx2+1,mx-1)
		//printf("%d: %lld %lld [mnlike=%d,mx=%d,mx2=%d]\n",at,a,b,mnlike,mx,mx2);
		ret+=a+b;
	}
	printf("%lld\n",ret);
}

int main() {
	run();
	return 0;
}

Submission Info

Submission Time
Task F - Black Radius
User krijgertje
Language C++14 (GCC 5.4.1)
Score 1900
Code Size 4178 Byte
Status AC
Exec Time 121 ms
Memory 22912 KB

Compile Error

./Main.cpp: In function ‘void run()’:
./Main.cpp:85:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
                ^
./Main.cpp:88:31: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   int a,b; scanf("%d%d",&a,&b); --a,--b;
                               ^
./Main.cpp:92:19: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s",slike);
                   ^

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 1300 / 1300 600 / 600
Status
AC × 3
AC × 31
AC × 81
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt
Subtask 0_01.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt, 2_23.txt, 2_24.txt, 2_25.txt, 2_26.txt, 2_27.txt, 2_28.txt, 2_29.txt, 2_30.txt, 2_31.txt, 2_32.txt, 2_33.txt, 2_34.txt, 2_35.txt, 2_36.txt, 2_37.txt, 2_38.txt, 2_39.txt, 2_40.txt, 2_41.txt, 2_42.txt, 2_43.txt, 2_44.txt, 2_45.txt, 2_46.txt, 2_47.txt
Case Name Status Exec Time Memory
0_00.txt AC 3 ms 256 KB
0_01.txt AC 3 ms 256 KB
0_02.txt AC 3 ms 256 KB
1_00.txt AC 3 ms 256 KB
1_01.txt AC 121 ms 22912 KB
1_02.txt AC 114 ms 18688 KB
1_03.txt AC 105 ms 11008 KB
1_04.txt AC 85 ms 11008 KB
1_05.txt AC 71 ms 11008 KB
1_06.txt AC 97 ms 11008 KB
1_07.txt AC 98 ms 11008 KB
1_08.txt AC 101 ms 12032 KB
1_09.txt AC 112 ms 16896 KB
1_10.txt AC 106 ms 16000 KB
1_11.txt AC 118 ms 22656 KB
1_12.txt AC 102 ms 13440 KB
1_13.txt AC 112 ms 16512 KB
1_14.txt AC 100 ms 13312 KB
1_15.txt AC 100 ms 12288 KB
1_16.txt AC 106 ms 14208 KB
1_17.txt AC 117 ms 15616 KB
1_18.txt AC 103 ms 10880 KB
1_19.txt AC 99 ms 10880 KB
1_20.txt AC 102 ms 11008 KB
1_21.txt AC 105 ms 11776 KB
1_22.txt AC 89 ms 11008 KB
1_23.txt AC 91 ms 11008 KB
1_24.txt AC 98 ms 11776 KB
1_25.txt AC 92 ms 12800 KB
1_26.txt AC 89 ms 10752 KB
1_27.txt AC 84 ms 11008 KB
1_28.txt AC 80 ms 11264 KB
1_29.txt AC 97 ms 13696 KB
2_00.txt AC 3 ms 256 KB
2_01.txt AC 114 ms 22400 KB
2_02.txt AC 117 ms 20608 KB
2_03.txt AC 116 ms 19968 KB
2_04.txt AC 108 ms 16768 KB
2_05.txt AC 111 ms 16896 KB
2_06.txt AC 115 ms 17792 KB
2_07.txt AC 99 ms 11008 KB
2_08.txt AC 99 ms 11008 KB
2_09.txt AC 106 ms 11008 KB
2_10.txt AC 81 ms 11008 KB
2_11.txt AC 81 ms 11008 KB
2_12.txt AC 83 ms 11008 KB
2_13.txt AC 73 ms 11008 KB
2_14.txt AC 69 ms 11008 KB
2_15.txt AC 71 ms 11008 KB
2_16.txt AC 94 ms 11008 KB
2_17.txt AC 94 ms 11008 KB
2_18.txt AC 96 ms 11008 KB
2_19.txt AC 94 ms 11008 KB
2_20.txt AC 95 ms 11008 KB
2_21.txt AC 99 ms 11008 KB
2_22.txt AC 99 ms 11904 KB
2_23.txt AC 98 ms 12416 KB
2_24.txt AC 103 ms 12288 KB
2_25.txt AC 109 ms 17408 KB
2_26.txt AC 110 ms 16128 KB
2_27.txt AC 119 ms 17280 KB
2_28.txt AC 102 ms 15616 KB
2_29.txt AC 118 ms 20992 KB
2_30.txt AC 111 ms 19200 KB
2_31.txt AC 108 ms 15744 KB
2_32.txt AC 95 ms 11392 KB
2_33.txt AC 103 ms 13440 KB
2_34.txt AC 105 ms 15232 KB
2_35.txt AC 107 ms 18048 KB
2_36.txt AC 98 ms 11008 KB
2_37.txt AC 102 ms 11008 KB
2_38.txt AC 101 ms 12032 KB
2_39.txt AC 104 ms 11264 KB
2_40.txt AC 85 ms 11008 KB
2_41.txt AC 93 ms 10880 KB
2_42.txt AC 80 ms 10880 KB
2_43.txt AC 103 ms 16640 KB
2_44.txt AC 80 ms 10880 KB
2_45.txt AC 75 ms 11008 KB
2_46.txt AC 75 ms 11136 KB
2_47.txt AC 95 ms 14336 KB