Submission #82804

# Submission time Handle Problem Language Result Execution time Memory
82804 2018-11-01T19:56:34 Z farukkastamonuda Inspection (POI11_ins) C++14
0 / 100
1320 ms 72424 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define lo long long 
#define inf 1000000009
#define md 1000000007
#define li 1000005
#define mp make_pair
#define pb push_back
using namespace std;
lo int n,x,y,ok[li],sz[li],d[li],mx;
vector<lo int> v[li];
bool vis[li];
void dfs(int node,int ata){
	sz[node]=0;
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go==ata) continue;
		dfs(go,node);
		sz[node]+=sz[go]+1;
	}
}
void dfs2(int node,int ata){
	lo int flag=0,sum=0;
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go==ata) continue;
		sum+=sz[go]+1;
		if(sz[go]+1>=(n+1)/2){
			flag=1;
		}
	}
	if(n-sum-1<(n+1)/2 && flag==0) ok[node]=1;
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go==ata) continue;
		dfs2(go,node);
	}
}
lo int bfs(int st){
	memset(vis,false,sizeof(vis));
	memset(d,0,sizeof(d));
	queue< pair<lo int,int> > q;
	q.push(mp(0,st));
	lo int genel=0;
	while(!q.empty()){
		pair<lo int,int> temp=q.front();
		q.pop();
		int seh=temp.se;
		lo int cst=temp.fi;
		if(vis[seh]) continue;
		d[seh]=cst;
		genel+=cst*2;
		vis[seh]=true;
		for(int i=0;i<(int)v[seh].size();i++){
			int go=v[seh][i];
			if(vis[go]==false) q.push(mp(cst+1,go));
		}
	}
	return genel;
}
void dfs3(int node,int ata,lo int dep){
	mx=max(mx,dep);
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go==ata) continue;
		dfs3(go,node,dep+1);
	}
}
int main(){
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		scanf("%lld %lld",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	if(n==1){
		printf("0\n");
		return 0;
	}
	dfs(1,-1);
	dfs2(1,-1);
	lo int ggmx=0;
	for(int i=1;i<=n;i++){
		if(ok[i]==0) printf("-1\n");
		else{
			lo int topla=bfs(i);
			dfs(i,-1);
			lo int istek=0;
			for(int j=0;j<(int)v[i].size();j++){
				int git=v[i][j];
				istek=max(istek,sz[git]+1);
			}
			for(int j=0;j<(int)v[i].size();j++){
				int git=v[i][j];
				if(istek!=sz[git]+1) continue;
				mx=0;
				dfs3(git,i,1);
				ggmx=max(ggmx,mx);
			}
			printf("%lld\n",topla-ggmx);
		}
	}
	return 0;
}

Compilation message

ins.cpp: In function 'int main()':
ins.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld",&n);
  ~~~~~^~~~~~~~~~~
ins.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld %lld",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 23900 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 24020 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 24020 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 34 ms 24924 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 46 ms 25832 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 28792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 562 ms 48300 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1306 ms 72180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1304 ms 72188 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1320 ms 72424 KB Output isn't correct
2 Halted 0 ms 0 KB -