QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#728378 | #9570. Binary Tree | ucup-team112# | RE | 1ms | 3640kb | C++20 | 4.9kb | 2024-11-09 15:03:56 | 2024-11-09 15:03:57 |
Judging History
answer
#include <bits/stdc++.h>
#include <bitset>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <random>
#include <set>
#include <type_traits>
#include <unordered_map>
using namespace std;
using ll = long long;
#define rep(i, n, m) for (ll(i) = (n); (i) < (m); (i)++)
#define rrep(i, n, m) for (ll(i) = (n); (i) > (m); (i)--)
const ll mod = 998244353;
const ll inf = 1e18;
const ll INF = 4e18 + 10;
using pll = pair<ll, ll>;
void pline(vector<int> lis) {
rep(i, 0, lis.size()) {
printf("%d", lis[i]);
if (i != lis.size() - 1)
printf(" ");
else
printf("\n");
}
}
void pline(vector<ll> lis) {
rep(i, 0, lis.size()) {
printf("%lld", lis[i]);
if (i != lis.size() - 1)
printf(" ");
else
printf("\n");
}
}
void pline2(vector<ll> lis) {
rep(i, 0, lis.size()) {
printf("%lld", lis[i]);
if (i != lis.size() - 1)
printf("");
else
printf("\n");
}
}
void pline(vector<pair<ll, ll>> lis) {
rep(i, 0, lis.size()) {
printf("/%lld,%lld/", lis[i].first, lis[i].second);
if (i != lis.size() - 1)
printf(" ");
else
printf("\n");
}
}
// 全方位1回目
void dfsA(ll v, ll p, vector<vector<ll>> &lis, vector<bool> &able, vector<ll> &csize) {
csize[v] = 1;
for (ll nex : lis[v]) {
if (able[nex] && (nex != p)) {
dfsA(nex, v, lis, able, csize);
csize[v] += csize[nex];
}
}
}
// 全方位2回目
void dfsB(ll v, ll p, vector<vector<ll>> &lis, vector<bool> &able, vector<ll> &csize,
vector<ll> &ablevs, vector<ll> ¢orid) {
ll cssum = 0;
vector<ll> cslis;
for (ll nex : lis[v]) {
if (able[nex] && (nex != p)) {
dfsB(nex, v, lis, able, csize, ablevs, centorid);
cslis.push_back(csize[nex]);
cssum += csize[nex];
}
}
cslis.push_back(ablevs.size() - 1 - cssum);
ll csmax = 0;
for (ll tmp : cslis) {
csmax = max(csmax, tmp);
}
if (csmax <= ablevs.size() / 2) {
centorid.push_back(v);
}
}
// 消去
void dfsC(ll v, ll p, vector<vector<ll>> &lis, vector<bool> &able) {
for (ll nex : lis[v]) {
if (able[nex] && (nex != p)) {
dfsC(nex, v, lis, able);
}
able[v] = false;
}
}
int main() {
ll TT;
cin >> TT;
rep(loop, 0, TT) {
ll n;
cin >> n;
vector<vector<ll>> lis(n);
rep(i, 0, n) {
ll x, y;
cin >> x >> y;
x--;
y--;
if (x != -1) {
lis[i].push_back(x);
lis[x].push_back(i);
}
if (y != -1) {
lis[i].push_back(y);
lis[y].push_back(i);
}
}
// 可能性がある頂点
vector<bool> able(n, true);
while (1) {
// ありうる頂点
vector<ll> ablevs(0);
rep(v, 0, n) {
if (able[v]) {
ablevs.push_back(v);
}
}
if (ablevs.size() == 1) {
cout << "! " << ablevs[0] + 1 << flush << endl;
break;
}
// 重心を求める
vector<ll> csize(n, 0);
vector<ll> centorid(0);
dfsA(ablevs[0], -1, lis, able, csize);
dfsB(ablevs[0], -1, lis, able, csize, ablevs, centorid);
if (centorid.size() == 2) {
cout << "? " << centorid[0] + 1 << " " << centorid[1] + 1 << flush << endl;
ll cat;
cin >> cat;
if (cat == 0) {
dfsC(centorid[1], centorid[0], lis, able);
} else {
dfsC(centorid[0], centorid[1], lis, able);
}
} else if (centorid.size() == 1) {
vector<ll> cvs;
for (ll nex : lis[centorid[0]]) {
if (able[nex]) {
cvs.push_back(nex);
}
}
cout << "? " << cvs[0] + 1 << " " << cvs[1] + 1 << flush << endl;
ll cat;
cin >> cat;
if (cat == 0) {
dfsC(cvs[1], centorid[0], lis, able);
able[centorid[0]] = false;
} else if (cat == 2) {
dfsC(cvs[0], centorid[0], lis, able);
able[centorid[0]] = false;
} else {
dfsC(cvs[1], centorid[0], lis, able);
dfsC(cvs[0], centorid[0], lis, able);
}
}
}
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3640kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0 2
output:
? 1 5 ? 2 4 ! 3 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Runtime Error
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 2 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 1 0 5 4 5 3 1 0 0 0 0 0 0 1 2 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 0 2
output:
? 8 2 ? 6 2 ? 7 6 ! 6 ? 7 3 ? 8 5 ? 7 5 ! 7 ? 8 1 ? 4 2 ? 8 6 ! 8 ? 4 5 ? 3 1 ! 1 ? 5 6 ? 5 1 ? 8 7 ! 7