Submission #81747

# Submission time Handle Problem Language Result Execution time Memory
81747 2018-10-26T12:15:03 Z farukkastamonuda Usmjeri (COCI17_usmjeri) C++14
0 / 140
685 ms 84964 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define lo long long 
#define inf 1000000000
#define md 1000000007
#define li 300005
#define mp make_pair
#define pb push_back
using namespace std;
int n,m,x,y,baba[22][li],dep[li],h[li],vis[li],say=1;
vector<int> v[li];
vector< pair<int,int> > v2[li];
void dfs(int node,int ata,int der){
	baba[0][node]=ata;
	dep[node]=der;
	for(int i=0;i<(int)v[node].size();i++){
		int go=v[node][i];
		if(go==ata) continue;
		dfs(go,node,der+1);
	}
}
void b_lca(){
	for(int i=1;i<=20;i++){
		for(int j=1;j<=n;j++){
			baba[i][j]=baba[i-1][baba[i-1][j]];
		}
	}
}
int f_lca(int x,int y){
	if(dep[x]<dep[y]) swap(x,y);
	int dx=dep[x];
	int dy=dep[y];
	for(int i=20;i>=0;i--){
		if(dx-(1<<i)>=dy){
			dx-=(1<<i);
			x=baba[i][x];
		}
	}
	if(x==y) return x;
	for(int i=20;i>=0;i--){
		if(baba[i][x]!=baba[i][y]){
			x=baba[i][x];
			y=baba[i][y];
		}
	}
	return baba[0][x];
}
int build(int x){
	for(int i=0;i<(int)v[x].size();i++){
		int go=v[x][i];
		if(go==baba[0][x]) continue;
		int val=build(go);
		h[x]=min(h[x],val);
		if(val<dep[x]){
			v2[x].pb(mp(go,0));
			v2[go].pb(mp(x,0));
		}
	}
	return h[x];
}
int dfs2(int node,int ata,int renk){
	if(vis[node]!=-1) return (vis[node]==renk);
	vis[node]=renk;
	for(int i=0;i<(int)v2[node].size();i++){
		int go=v2[node][i].fi;
		int deg=v2[node][i].se;
		if(go==ata) continue;
		if(dfs2(go,node,renk^deg)==0){
			return 0;
		}
	}
	return 1;
}
int main(){
	memset(vis,-1,sizeof(vis));
	scanf("%d %d",&n,&m);
	for(int i=1;i<n;i++){
		scanf("%d %d",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	dfs(1,-1,0);
	b_lca();
	for(int i=1;i<=m;i++){
		scanf("%d %d",&x,&y);
		int lca=f_lca(x,y);
		if(lca!=x && lca!=y){
			v2[x].pb(mp(y,1));
			v2[y].pb(mp(x,1));
		}
		h[x]=min(h[x],dep[x]);
		h[y]=min(h[y],dep[y]);
	}
	build(1);
	for(int i=1;i<=n;i++){
		if(vis[i]==-1){
			if(dfs2(i,-1,0)==0){
				printf("0\n");
				return 0;
			}
			say=(say*2ll)%md;
		}
	}
	printf("%d\n",say);
	return 0;
}

Compilation message

usmjeri.cpp: In function 'int main()':
usmjeri.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~
usmjeri.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~
usmjeri.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 190 ms 38760 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 355 ms 84964 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 84964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 84964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 84964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 84964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 547 ms 84964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 685 ms 84964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 645 ms 84964 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 668 ms 84964 KB Output isn't correct
2 Halted 0 ms 0 KB -