QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#358754#8276. Code CongestionPPP#TL 0ms0kbC++142.3kb2024-03-19 23:36:482024-03-19 23:36:49

Judging History

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

  • [2024-03-19 23:36:49]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-03-19 23:36:48]
  • 提交

answer

//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

#define X first
#define Y second


const ll mod = 1000000007;
//const ll mod = 998244353;


const int mx = 123000;
vector<int> g[mx];
int h[mx], up[mx], was[mx];

int dist(int u, int v)
{
    int D = 0;
    while (u!=v)
    {
        D++;
        if (h[u]>=h[v]) u = up[u];
        else v = up[v];
    }
    return D;
}

void dfs0(int u, int p)
{
    up[u] = p;
    for (int v: g[u])
    {
        if (v==p) continue;
        h[v] = h[u]+1;
        dfs0(v,u);
    }
}

vector<int> ord;

void dfs(int u, int W)
{
    was[u] = 1;
    if (W%2==0) ord.push_back(u);
    for (int v: g[u])
    {
        if (was[v]==1) continue;
        dfs(v,W^1);
    }
    if (W%2!=0) ord.push_back(u);
}

void solve()
{
    int n, s, t;
    cin >> n >> s >> t;
    s--, t--;
    for (int i=0;i<n;i++) g[i].clear();
    for (int i=1;i<n;i++)
    {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    h[s] = 0;
    dfs0(s,-1);
    for (int i=0;i<n;i++) was[i] = 0;
    vector<int> a;
    int u = t;
    while (u!=-1)
    {
        was[u] = 1;
        a.push_back(u);
        u = up[u];
    }
    reverse(a.begin(),a.end());
    ord.clear();
    for (int i=0;i<a.size();i++)
    {
        if (a[i]!=t) dfs(a[i],0);
        else dfs(a[i],1);
    }
    int A = 0;
    assert(ord.size()==n);
    for (int i=0;i+1<n;i++)
    {
        int D = dist(ord[i],ord[i+1]);
        assert(D<=3);
        A ^= D;
    }
    if (A<=1)
    {
        for (int x: ord) cout << x+1 << " ";
        cout << "\n";
        return;
    }
    for (int i=1;i+2<n;i++)
    {
        int W = dist(ord[i],ord[i-1]);
        W ^= dist(ord[i+1],ord[i+2]);
        W ^= dist(ord[i+1],ord[i-1]);
        W ^= dist(ord[i],ord[i+2]);
        if (W==2)
        {
            swap(ord[i],ord[i+1]);
            for (int x: ord) cout << x+1 << " ";
            cout << "\n";
            return;
        }
    }
    for (int x: ord) cout << x+1 << " ";
    cout << "\n";
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll T;
    T = 1;
    cin >> T;
    while (T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

3 3
2 3 4
1 2 2

output:


result: