题解:AT_abc398_e [ABC398E] Tree Game
SunburstFan · · 题解
E - Tree Game
题目大意
给定一棵有
解题思路
用 BFS 为每个顶点染色,以区分出两个不交叠的颜色集合(把树看做二分图)。然后,对于颜色不同且尚未直接相连的一对节点
代码:
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
vector<vector<int>>g(n+1);
vector<vector<bool>>e(n+1,vector<bool>(n+1,0));
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
e[u][v]=e[v][u]=1;
}
vector<int>c(n+1,-1);
queue<int>q;
c[1]=0;
q.push(1);
while(!q.empty()){
int u=q.front();
q.pop();
for(auto v:g[u]){
if(c[v]==-1){
c[v]=c[u]^1;
q.push(v);
}
}
}
set<pair<int,int>>m;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(c[i]!=c[j]&&!e[i][j]){
m.insert({i,j});
}
}
}
bool f=(m.size()%2==1);
if(f){
cout<<"First\n";
cout.flush();
}
else{
cout<<"Second\n";
cout.flush();
}
bool t=f;
while(1){
if(t){
if(m.empty())break;
auto p=*m.begin();
m.erase(m.begin());
cout<<p.first<<" "<<p.second<<"\n";
cout.flush();
}
int u,v;
cin>>u>>v;
if(u==-1&&v==-1)break;
pair<int,int>p=(u<v)?make_pair(u,v):make_pair(v,u);
if(m.find(p)!=m.end()){
m.erase(p);
}
t=1;
}
return 0;
}