这题竟然是二分染色
先将树进行二分染色,可以发现,当两个点颜色不同时,将他们相连不会产生 odd cycle。
所以统计颜色为1和0的点,然后进行组合。每次要进行一个操作时,操作必定是所有组合中的一个。
代码如下:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<set>
#define x first
#define y second
using namespace std;
const int N = 110, M = 220;
typedef pair<int,int> PII;
int head[N], ne[M], to[M], idx;
int n;
int map[N][N];
bool st[N];
vector<int> ty[2];
void add(int a,int b)
{
ne[idx] = head[a], head[a] = idx, to[idx] = b, idx ++;
}
void dfs(int u,int f,int type)
{
ty[type].push_back(u);
for(int i = head[u];i!=-1;i = ne[i]){
int j = to[i];
if(j!=f){
dfs(j,u,(type+1)%2);
}
}
}
int main()
{
cin>>n;
memset(head,-1,sizeof head);
for(int i = 1;i<n;i++){
int a,b;
cin>>a>>b;
add(a,b);
add(b,a);
map[a][b] = 1;
map[b][a] = 1;
}
dfs(1,0,0);
set<PII> s;
int cnt = 0;
for(int i = 0;i<ty[0].size();i++)
for(int j = 0;j<ty[1].size();j++)
{
int a = ty[0][i], b = ty[1][j];
if(a>b) swap(a,b);
if(!map[a][b]) {
cnt ++;
s.insert({a,b});
}
}
if(cnt%2){
cout<<"First"<<endl;
while(1){
int a,b;
auto it = *s.begin();
s.erase({it.x,it.y});
cout<<it.x<<' '<<it.y<<endl;
cin>>a>>b;
if(a == -1 && b == -1) break;
else s.erase({a,b});
}
}
else{
cout<<"Second"<<endl;
while(1){
int a,b;
cin>>a>>b;
if(a == -1 && b == -1) break;
else s.erase({a,b});
auto it = *s.begin();
s.erase({it.x,it.y});
cout<<it.x<<' '<<it.y<<endl;
}
}
return 0;
}