QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#735127 | #9713. Kill the tree | SSAABBEERR | WA | 104ms | 35336kb | C++20 | 2.1kb | 2024-11-11 17:38:07 | 2024-11-11 17:38:07 |
Judging History
answer
#include<bits/stdc++.h>
#define lowbit(x) (x & (-x))
#define endl "\n"
#define ll long long
#define int long long
#define ull unsigned long long
#define fi first
#define se second
using namespace std;
#define pii pair<int, int>
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define pre(i, a, b) for(int i = a; i >= b; i -- )
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
mt19937_64 rnd(time(0));
const int N = 1e6 + 10;
int n;
vector<int>p[N];
int sz[N], f[N], depth[N], ma[N];
pair<int, int>ans[N];
void dfs1(int u, int fa)
{
sz[u] = 1, f[u] = fa, depth[u] = depth[fa] + 1;
for(auto j : p[u])
{
if(j == fa) continue;
dfs1(j, u);
sz[u] += sz[j];
if(sz[j] > ma[u]) ma[u] = sz[j];
}
}
void dfs2(int u, int fa)
{
int ff = 0;
for(auto j : p[u])
{
if(j == fa) continue;
dfs2(j, u);
ff = 1;
}
if(!ff)
{
ans[u].first = u;
return;
}
for(auto j : p[u])
{
if(j == fa) continue;
int d = ans[j].first;
if(ans[j].second && depth[ans[j].second] > depth[d]) d = ans[j].second;
int e = 0;
while(1)
{
int w = ma[d];
w = max(w, sz[u] - sz[d]);
if(w * 2 <= sz[u])
{
if(ans[u].first == d || ans[u].second == d) break;
if(!ans[u].first) ans[u].first = d;
else ans[u].second = d;
e = 1;
}
else if(e) break;
if(d == u) break;
d = f[d];
if(ans[u].first && ans[u].second) break;
}
}
}
void solve()
{
cin >> n;
rep(i, 1, n - 1)
{
int u, v;
cin >> u >> v;
p[u].push_back(v);
p[v].push_back(u);
}
dfs1(1, 0);
dfs2(1, 0);
rep(u, 1, n)
{
cout << ans[u].first << " ";
if(ans[u].second) cout << ans[u].second;
cout << endl;
}
}
signed main()
{
IOS;
int _;
_ = 1;
// cin >> _;
while(_ -- )
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 104ms
memory: 35336kb
input:
200000 42924 18271 60536 154257 107175 95680 196335 71607 158051 155259 110869 30974 143595 43516 4228 138046 26249 183847 123888 199873 15009 25362 166527 175160 15257 67159 124779 199363 148904 54047 5988 18123 58281 40739 44562 143880 72819 132492 137782 29662 130741 78276 55073 93292 82700 18328...
output:
1 134385 143670 45886 126220 106649 108393 44121 95313 5226 116080 147311 141180 147983 7428 74120 161963 3879 178499 162544 171549 144413 127262 188325 133093 171802 43558 28179 125217 140604 186651 176397 45633 26425 26982 3745 30063 128965 148036 19661 141733 115691 3491 8560 360...
result:
wrong answer 3rd numbers differ - expected: '45886', found: '143670'