QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#735130#9713. Kill the treeSSAABBEERRWA 117ms35128kbC++202.2kb2024-11-11 17:39:312024-11-11 17:39:31

Judging History

你现在查看的是最新测评结果

  • [2024-11-11 17:39:31]
  • 评测
  • 测评结果:WA
  • 用时:117ms
  • 内存:35128kb
  • [2024-11-11 17:39:31]
  • 提交

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)
    {
        if(ans[u].first && ans[u].second) swap(ans[u].first, ans[u].second);
        cout << ans[u].first << " ";
        if(ans[u].second) cout << ans[u].second;
        cout << endl;
    }
}
signed main()
{
    IOS;
    int _;
    _ = 1;
    // cin >> _;
    while(_ -- )
    {
        solve();
    }
	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 117ms
memory: 35128kb

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:

134385 1
45886 143670
106649 126220
44121 108393
5226 95313
147311 116080
141180 
147983 
74120 7428
161963 
3879 
178499 
171549 162544
144413 
127262 
133093 188325
171802 
43558 
28179 
140604 125217
186651 
45633 176397
26425 
3745 26982
30063 
128965 
19661 148036
141733 
115691 
3491 
36057 85...

result:

wrong answer 1st numbers differ - expected: '1', found: '134385'