답안 #1106902

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1106902 2024-10-31T09:01:38 Z PieArmy 동기화 (JOI13_synchronization) C++17
20 / 100
97 ms 17244 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;}

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(tip[i]==1)las[i]=m;
	}
	deco(1);
	while(q--){
		int x;cin>>x;
		cout<<ans[x]<<endl;
	}
}

int 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(int)':
synchronization.cpp:71:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<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: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |  for(int i=0;i<komsu[pos].size();i++){
      |              ~^~~~~~~~~~~~~~~~~~
synchronization.cpp: In function 'int 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 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4432 KB Output is correct
4 Correct 1 ms 4596 KB Output is correct
5 Incorrect 1 ms 4432 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 12420 KB Output is correct
2 Correct 72 ms 14152 KB Output is correct
3 Correct 72 ms 17224 KB Output is correct
4 Correct 72 ms 17244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4432 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 97 ms 15944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Incorrect 1 ms 4432 KB Output isn't correct
5 Halted 0 ms 0 KB -