QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#746849 | #9733. Heavy-light Decomposition | SSAABBEERR | WA | 0ms | 11780kb | C++20 | 1.4kb | 2024-11-14 15:42:35 | 2024-11-14 15:42:37 |
Judging History
answer
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define pre(i, a, b) for(int i = a; i >= b; i -- )
using namespace std;
const int N = 1e6 + 10;
int n, m, k;
int cnt[N];
int l[N], r[N], f[N], son[N];
void solve()
{
cin >> n >> m;
rep(i, 1, n) cnt[i] = 0, f[i] = 0;
rep(i, 1, m)
{
cin >> l[i] >> r[i];
}
int w = 0, rt = 0, tt;
rep(i, 1, m)
{
if(cnt[l[i]] || cnt[r[i]])
{
cout << "IMPOSSIBLE" << endl;
return;
}
cnt[l[i]] = 1;
cnt[r[i]] = 1;
if(l[i] == r[i]) w ++ ;
else rt = l[i], tt = r[i];
}
if(w == n)
{
cout << "IMPOSSIBLE" << endl;
return;
}
rep(i, 1, m)
{
if(l[i] == rt) continue;
else if(l[i] != r[i]) f[l[i]] = rt, f[r[i]] = l[i];
else f[l[i]] = rt;
}
f[tt] = rt;
rep(i, 1, n)
{
if(!f[i] && i != rt) f[i] = f[tt], f[tt] = i;
}
// cout<<rt;
rep(i, 1, n) cout << f[i] << " ";
// rep(i, 1, n) cout << i << " " << f[i] << endl;
cout << endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
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: 0ms
memory: 11780kb
input:
3 12 5 1 5 9 11 7 8 6 6 12 12 4 3 1 1 4 4 2 3 2 2 1 1 2 2
output:
7 7 2 3 1 7 0 10 7 4 9 7 2 0 2 2 IMPOSSIBLE
result:
wrong answer 2's parent should be 1, but 7 found (test case 1)