답안 #1106897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1106897 2024-10-31T08:54:15 Z PieArmy 동기화 (JOI13_synchronization) C++17
0 / 100
76 ms 17736 KB
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}
#define int ll
int n,m,q;
vector<pair<int,int>>komsu[100001];
int fir[100001],las[100001],tip[100001],sub[100001],ans[100001];

void dfs(int pos,int par){
	sub[pos]=1;
	for(pair<int,int>x:komsu[pos]){
		if(sub[x.fr]==-1)continue;
		if(x.fr==par)continue;
		dfs(x.fr,pos);
		sub[pos]+=sub[x.fr];
	}
}

void deco(int pos){
	dfs(pos,pos);
	int total=sub[pos];
	while(true){
		int nex=-1;
		for(auto x:komsu[pos]){
			if(sub[x.fr]==-1)continue;
			if(sub[x.fr]>sub[pos])continue;
			if(sub[x.fr]>total/2){
				nex=x.fr;
				break;
			}
		}
		if(nex==-1)break;
		pos=nex;
	}
	sub[pos]=-1;
	ans[pos]++;
	sort(komsu[pos].begin(),komsu[pos].end(),[&](const pair<int,int>&x,const pair<int,int>&y){
		if((sub[x.fr]==-1)==(sub[y.fr]==-1))return fir[x.sc]<fir[y.sc];
		return sub[x.fr]>sub[y.fr];
	});
	vector<int>pref;
	for(auto x:komsu[pos]){
		if(sub[x.fr]==-1)break;
		if(fir[x.sc]==sonsuz)break;
		pref.pb(0);
		queue<pair<int,int>>q;
		q.push(x);
		while(q.size()){
			int loc=q.front().fr,prev=q.front().sc;
			q.pop();
			ans[pos]++;
			pref.back()++;
			for(auto y:komsu[loc]){
				if(sub[y.fr]==-1)continue;
				if(y.sc==prev)continue;
				if(fir[y.sc]<=las[prev]){
					q.push(y);
				}
			}
		}
	}
	for(int i=1;i<pref.size();i++){
		pref[i]+=pref[i-1];
	}

	for(int i=0;i<komsu[pos].size();i++){
		pair<int,int>x=komsu[pos][i];
		if(sub[x.fr]==-1)break;
		if(fir[x.sc]==sonsuz)break;
		int l=0,r=pref.size()-1;
		while(l<r){
			if(fir[komsu[pos][(l+r+1)/2].sc]<=las[x.sc])l=(l+r+1)/2;
			else r=((l+r+1)/2)-1;
		}
		queue<pair<int,int>>q;
		q.push(x);
		while(q.size()){
			int loc=q.front().fr,prev=q.front().sc;
			q.pop();
			ans[loc]+=pref[l]-pref[i]+(i==0?0:pref[i-1])+1;
			for(auto y:komsu[loc]){
				if(sub[y.fr]==-1)continue;
				if(y.sc==prev)continue;
				if(fir[prev]<=las[y.sc]){
					q.push(y);
				}
			}
		}
	}
	
	for(auto x:komsu[pos]){
		if(sub[x.fr]==-1)continue;
		deco(x.fr);
	}
}

void code(){
	cin>>n>>m>>q;
	fir[0]=0;
	las[0]=sonsuz-1;
	for(int i=1;i<n;i++){
		fir[i]=sonsuz;
		las[i]=-1;
		int x,y;cin>>x>>y;
		komsu[x].pb({y,i});
		komsu[y].pb({x,i});
	}
	for(int i=1;i<=m;i++){
		int x;cin>>x;
		tip[x]^=1;
		if(tip[x]==0){
			las[x]=i-1;
		}
		else if(fir[x]==sonsuz){
			fir[x]=i;
		}
	}
	for(int i=1;i<n;i++){
		if(las[i]==-1)las[i]=m;
	}
	deco(1);
	while(q--){
		int x;cin>>x;
		cout<<ans[x]<<endl;
	}
}

int32_t main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
	int t=1;
	if(!t)cin>>t;
	while(t--){code();cout<<endl;}
	return 0;
}

Compilation message

synchronization.cpp: In function 'void deco(ll)':
synchronization.cpp:71:15: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for(int i=1;i<pref.size();i++){
      |              ~^~~~~~~~~~~~
synchronization.cpp:75:15: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |  for(int i=0;i<komsu[pos].size();i++){
      |              ~^~~~~~~~~~~~~~~~~~
synchronization.cpp: In function 'int32_t main()':
synchronization.cpp:139:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |  bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~
synchronization.cpp:139:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |  bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6480 KB Output is correct
2 Correct 2 ms 6480 KB Output is correct
3 Incorrect 2 ms 6480 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 64 ms 14980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 6480 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 76 ms 17736 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6656 KB Output is correct
2 Correct 2 ms 6480 KB Output is correct
3 Incorrect 2 ms 6696 KB Output isn't correct
4 Halted 0 ms 0 KB -