QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#769392 | #9570. Binary Tree | myy | TL | 0ms | 0kb | C++20 | 4.7kb | 2024-11-21 17:27:40 | 2024-11-21 17:27:45 |
answer
#include <iostream>
#include <vector>
#include<string>
#include<cstring>
#include<map>
#include<stack>
#include<algorithm>
#include<queue>
#include<set>
#include<cstdio>
#include<ctime>
#include<cmath>
#include<unordered_map>
using namespace std;
#define N 500005
#define endl '\n'
#define int long long
#define mod 1000000000000
#define _(x) cout<<"["<<#x<<"]=["<<x<<"]\n"
int n, m, t, k, q, x, y, z;
void ok() {
cout << "---ok---\n";
}
void debug(string x, int y) {
cout << "[" << x << "]=[" << y << "]" << endl;
}
void debug(string x, int y[]) {
cout << "[" << x << "] = [";
for (int i = 0; i < 10; i++)cout << y[i] << " ";
cout << "]" << endl;
}
void debug(string x, vector<int>y) {
cout << "[" << x << "] = [";
for (auto i : y)cout << i << " ";
cout << "]" << endl;
}
void debug(string x, map<int, int>y) {
cout << "[" << x << "] = [";
for (auto [i, j] : y)cout << "(" << i << "," << j << ") ";
cout << "]" << endl;
}
vector<int>v[N];
int szp[N], sum, maxp[N], rt = 0; // szp:含自己子节点和,maxp:删除p后树的最大联通的结点数,sum:树结点总数
void getmaxp(int p, int fa) {
szp[p] = 1;
maxp[p] = 0;
for (auto i: v[p]) {
if (i == fa)continue;
if (i == 0)continue;//
getmaxp(i, p);
szp[p] += szp[i];
maxp[p] = max(maxp[p], szp[i]);
}
maxp[p] = max(maxp[p], sum - szp[p]);
//cout << "#" << p << " " << rt << " " << maxp[p] <<" "<< maxp[rt] << endl;
if (maxp[p] < maxp[rt])rt = p;
}
void getrt(int p, int fa) {
for (auto i : v[p]) {
if (i == fa)continue;
if (i == 0)continue;//
if (maxp[rt] > maxp[i])rt = i;
getrt(i, p);
}
}
int dfs(int x,int fa) {
int cnt = 1;
for (int i = 0;i < 3;i++) {
if ( v[x][i] == fa)continue;
if (v[x][i]) {
cnt += dfs(v[x][i],x);
}
}
return cnt;
}
bool cmp(int a, int b) {
return maxp[a] < maxp[b];
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int test = 1;
cin >> test;
while (test--) {
cin >> n;
for (int i = 1;i <= n;i++) {
v[i].clear();
for (int j = 0;j < 3;j++)v[i].push_back(0);
}
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= 2;j++) {
cin >> x;
if (x) {
v[i][j] = x;
v[x][0] = i;
}
}
}
sum = n;
rt = 1;
while (sum > 1) {
getmaxp(rt, 0);
getrt(rt, 0);
// _(rt);
int mi = -1;
int cnt = 0;
for (int i=0;i<=2;i++) {
if (v[rt][i]) {
cnt++;
if (mi == -1)mi = i;
else if (maxp[v[rt][mi]] > maxp[v[rt][i]]) {
mi = i;
}
}
}
// debug("maxp", maxp);
if (cnt == 1) {
cout << "? " << v[rt][mi] << " " << rt << endl;
cin >> k;
if (k < 1) {
for (int i = 0;i < 3;i++)if (v[v[rt][mi]][i] == rt)v[v[rt][mi]][i] = 0;
rt = v[rt][mi];
}
else {
for (int i = 0;i < 3;i++)if (v[rt][i] == v[rt][mi])v[rt][i] = 0;
//rt = v[rt][mi];
}
sum = dfs(rt, 0);
}
else {
vector<int>c;
for (int i = 0;i <= 2;i++) {
if (v[rt][i]) {
c.push_back(v[rt][i]);
}
}
sort(c.begin(), c.end(), cmp);
cout << "? " << c[0] << " " << c[1] << endl;
cin >> k;
if (k < 1) {
for (int i = 0;i < 3;i++)if (v[c[0]][i] == rt)v[c[0]][i] = 0;
rt = c[0];
}
else if (k == 1) {
for (int i = 0;i < 3;i++)if (v[rt][i] == c[0])v[rt][i] = 0;
for (int i = 0;i < 3;i++)if (v[rt][i] == c[1])v[rt][i] = 0;
}
else {
for (int i = 0;i < 3;i++)if (v[c[1]][i] == rt)v[c[1]][i] = 0;
rt = c[1];
}
sum = dfs(rt, 0);
}
// _(sum);
}
cout << "! " << rt << endl;
cout.flush();
}
return 0;
}
/*
3
11
2 3
4 5
0 10
6 7
0 0
8 9
0 0
0 0
0 0
0 11
0 0
5
0 0
1 5
2 4
0 0
0 0
2
0 2
0 0
*/
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
2 5 0 0 1 5 2 4 0 0 0 0