QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#562803 | #3948. Equilibrium | LaVuna47 | WA | 0ms | 3664kb | C++20 | 3.1kb | 2024-09-13 21:05:39 | 2024-09-13 21:05:41 |
Judging History
answer
/** gnu specific **/
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/** contains everything I need in std **/
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(S) ((int)S.size())
#define FOR(i, n) for(int i = 0; i < n; ++i)
#define RFOR(i, n) for(int i = n-1; i >= 0; --i)
#define output_vec(vec) { FOR(i_, sz(vec)) cout << vec[i_] << ' '; cout << '\n'; }
#define x first
#define y second
#define pb push_back
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<bool> vb;
typedef short si;
typedef unsigned long long ull;
typedef long double LD;
typedef pair<ull, ull> pull;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using namespace std;
#ifdef ONPC
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
int L, R;
int get_greater()
{
return ++R;
}
int get_smaller()
{
return --L;
}
void f(int v, int pr, const vector<vector<int>>& G, vector<int>& a)
{
if(a[v] == -1)
{
a[v] = get_greater();
}
if(sz(G[v]) % 2 == 0)
{
int par = 0;
if(a[pr] > a[v])
{
++par;
}
int set_ctr = 0;
for(auto to: G[v])
{
if(a[to] == -1)
{
if(par++ % 2 == 0)
a[to] = get_greater();
else
a[to] = get_smaller();
}
else ++set_ctr;
}
assert(set_ctr <= 1);
}
for(auto to: G[v])
{
if(to != pr)
{
f(to, v, G, a);
}
}
}
bool check(vector<vector<int>>& G, int n, vector<int>& a)
{
int k = 0;
FOR(i, n)
{
if(sz(G[i]) % 2 == 0)
++k;
}
FOR(i, n)
{
if(sz(G[i]) % 2 == 0)
{
int ctr = 0;
for(auto to: G[i])
{
if(a[to] > a[i])
++ctr;
else
--ctr;
}
if(ctr != 0)
return false;
else
--k;
}
}
return k == 0;
}
int solve()
{
int n;
if(!(cin >> n))
return 1;
vector<vector<int>> G(n);
FOR(_, n-1)
{
int u, v;
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
L = 10000000, R = 10000000+1;
vector<int> a(n, -1);
a[0] = get_greater();
f(0, -1, G, a);
vector<int> res(n);
iota(all(res), 0);
sort(all(res), [&](int v1, int v2) -> bool {
return a[v1] < a[v2];
});
vector<int> marker(n);
FOR(i, n)
{
marker[res[i]] = i;
}
check(G, n, marker);
FOR(i, n) cout << marker[i] << ' ';
cout << '\n';
return 0;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int TET = 1e9;
//cin >> TET;
for (int i = 1; i <= TET; i++)
{
if (solve())
{
break;
}
#ifdef ONPC
cout << "__________________________" << endl;
#endif
}
#ifdef ONPC
cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3664kb
input:
3 0 1 0 2
output:
1 2 0
result:
wrong answer not optimal: 4 VS 2