답안 #291190

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
291190 2020-09-04T21:00:31 Z TadijaSebez Spring cleaning (CEOI20_cleaning) C++11
100 / 100
409 ms 17544 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=200050;
int deg[N];
vector<int> E[N];
const int M=2*N;
int root,ls[M],rs[M],sum[M],tsz,lzy[M];
void Flip(int&c,int ss,int se,int qs,int qe){
	if(qs>qe||qs>se||ss>qe)return;
	if(!c)c=++tsz;
	if(qs<=ss&&qe>=se){sum[c]=se-ss+1-sum[c];lzy[c]^=1;return;}
	int mid=ss+se>>1;
	Flip(ls[c],ss,mid,qs,qe);
	Flip(rs[c],mid+1,se,qs,qe);
	sum[c]=sum[ls[c]]+sum[rs[c]];
	if(lzy[c])sum[c]=se-ss+1-sum[c];
}
int head[N],lid[N],rid[N],tid,sz[N],par[N];
void DFS(int u,int p){
	sz[u]=1;par[u]=p;
	for(int v:E[u])if(v!=p)DFS(v,u),sz[u]+=sz[v];
}
void HLD(int u,int p){
	lid[u]=++tid;
	if(!head[u])head[u]=u;
	int hc=0;
	for(int v:E[u])if(v!=p&&sz[hc]<sz[v])hc=v;
	if(hc!=0){
		head[hc]=head[u];
		HLD(hc,u);
		for(int v:E[u])if(v!=p&&v!=hc)HLD(v,u);
	}
	rid[u]=tid;
}
void Flip(int u){
	while(u>0){
		Flip(root,2,tid,lid[head[u]],lid[u]);
		u=par[head[u]];
	}
}
int main(){
	int n,q;
	scanf("%i %i",&n,&q);
	for(int i=1,u,v;i<n;i++){
		scanf("%i %i",&u,&v);
		E[u].pb(v);E[v].pb(u);
		deg[u]++;deg[v]++;
	}
	DFS(1,0);
	HLD(1,0);
	int leaf=0;
	for(int i=1;i<=n;i++)if(deg[i]==1)Flip(i),leaf++;
	for(int i=1;i<=q;i++){
		int k;scanf("%i",&k);
		vector<int> p(k);
		for(int&j:p){
			scanf("%i",&j);
			deg[j]++;
			if(deg[j]!=2){
				leaf++;
				Flip(j);
			}
		}
		if(leaf%2==1)printf("-1\n");
		else printf("%i\n",n-1+k+n-1-sum[root]);
		reverse(p.begin(),p.end());
		for(int j:p){
			deg[j]--;
			if(deg[j]!=1){
				leaf--;
				Flip(j);
			}
		}
	}
	return 0;
}

Compilation message

cleaning.cpp: In function 'void Flip(int&, int, int, int, int)':
cleaning.cpp:13:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 |  int mid=ss+se>>1;
      |          ~~^~~
cleaning.cpp: In function 'int main()':
cleaning.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   44 |  scanf("%i %i",&n,&q);
      |  ~~~~~^~~~~~~~~~~~~~~
cleaning.cpp:46:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   46 |   scanf("%i %i",&u,&v);
      |   ~~~~~^~~~~~~~~~~~~~~
cleaning.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   55 |   int k;scanf("%i",&k);
      |         ~~~~~^~~~~~~~~
cleaning.cpp:58:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   58 |    scanf("%i",&j);
      |    ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 165 ms 6776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 5632 KB Output is correct
2 Correct 54 ms 5632 KB Output is correct
3 Correct 98 ms 14028 KB Output is correct
4 Correct 113 ms 11760 KB Output is correct
5 Correct 139 ms 14464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 6016 KB Output is correct
2 Correct 68 ms 6140 KB Output is correct
3 Correct 90 ms 16960 KB Output is correct
4 Correct 174 ms 17544 KB Output is correct
5 Correct 66 ms 15712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 7340 KB Output is correct
2 Correct 71 ms 6784 KB Output is correct
3 Correct 30 ms 6648 KB Output is correct
4 Correct 21 ms 6952 KB Output is correct
5 Correct 25 ms 7288 KB Output is correct
6 Correct 84 ms 7060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 287 ms 10744 KB Output is correct
2 Correct 394 ms 10488 KB Output is correct
3 Correct 273 ms 7928 KB Output is correct
4 Correct 381 ms 10468 KB Output is correct
5 Correct 380 ms 10488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 409 ms 13944 KB Output is correct
2 Correct 222 ms 15904 KB Output is correct
3 Correct 294 ms 15224 KB Output is correct
4 Correct 256 ms 16120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 165 ms 6776 KB Output is correct
3 Correct 54 ms 5632 KB Output is correct
4 Correct 54 ms 5632 KB Output is correct
5 Correct 98 ms 14028 KB Output is correct
6 Correct 113 ms 11760 KB Output is correct
7 Correct 139 ms 14464 KB Output is correct
8 Correct 69 ms 6016 KB Output is correct
9 Correct 68 ms 6140 KB Output is correct
10 Correct 90 ms 16960 KB Output is correct
11 Correct 174 ms 17544 KB Output is correct
12 Correct 66 ms 15712 KB Output is correct
13 Correct 105 ms 7340 KB Output is correct
14 Correct 71 ms 6784 KB Output is correct
15 Correct 30 ms 6648 KB Output is correct
16 Correct 21 ms 6952 KB Output is correct
17 Correct 25 ms 7288 KB Output is correct
18 Correct 84 ms 7060 KB Output is correct
19 Correct 287 ms 10744 KB Output is correct
20 Correct 394 ms 10488 KB Output is correct
21 Correct 273 ms 7928 KB Output is correct
22 Correct 381 ms 10468 KB Output is correct
23 Correct 380 ms 10488 KB Output is correct
24 Correct 409 ms 13944 KB Output is correct
25 Correct 222 ms 15904 KB Output is correct
26 Correct 294 ms 15224 KB Output is correct
27 Correct 256 ms 16120 KB Output is correct
28 Correct 306 ms 10104 KB Output is correct
29 Correct 284 ms 15224 KB Output is correct
30 Correct 143 ms 14472 KB Output is correct
31 Correct 180 ms 17528 KB Output is correct
32 Correct 393 ms 10492 KB Output is correct
33 Correct 231 ms 13304 KB Output is correct
34 Correct 257 ms 15356 KB Output is correct
35 Correct 258 ms 15224 KB Output is correct