QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#621951#6421. Degree of Spanning Treegates_orzRE 0ms9732kbC++203.4kb2024-10-08 18:38:122024-10-08 18:38:13

Judging History

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

  • [2024-10-08 18:38:13]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:9732kb
  • [2024-10-08 18:38:12]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

typedef long long LL;
//#define int LL
#define inl inline
const int N = 4e5 + 10;
const int M = N * 2;
//const int mod=998244353;
const int mod = 1000000007;
const double eps = 1e-8;
//const int mod=1e9+7;
typedef pair<int, int> PII;
//const int INF=0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
int n, m;

void become_faster() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

struct Node {
    int u,v;
    int id;
};
Node edge[N];
struct DSU {
    vector<int> fa, siz;

    DSU() {
    }

    DSU(int n) {
        init(n);
    }

    void init(int n) {
        fa.resize(n);
        iota(fa.begin(), fa.end(), 0);
        siz.assign(n, 1);
    }

    int find(int x) {
        while (x != fa[x]) {
            x = fa[x] = fa[fa[x]];
        }
        return x;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        siz[x] += siz[y];
        fa[y] = x;
        return true;
    }

    int size(int x) {
        return siz[find(x)];
    }
};
int dep[N];
int d[N];
DSU dsu;
vector<Node>vec[N];
void dfs(int u,int fat,int t) {
    dsu.fa[u]=t;
    dep[u]=dep[fat]+1;
    for(auto [u,v,id]:vec[u]) {
        if(v==fat)continue;
        dfs(v,u,t);
    }
}

void solve() {
    cin>>n>>m;
    dsu.init(n+1);
    for(int i=0;i<=n;i++) {
        d[i]=0;
        vec[i].clear();
    }

    vector<int>vis(m+1,0);
    vector<int>to(m+1);

    for(int i=1;i<=m;i++) {
        auto &[u,v,id]=edge[i];
        cin>>u>>v;
        id=i;
    }

    for(int i=1;i<=m;i++) {
        auto [u,v,id]=edge[i];
        if(!dsu.same(u,v)) {
            dsu.merge(u,v);
            d[u]++;
            d[v]++;
            vec[u].push_back({u,v,id});
            vec[v].push_back({v,u,id});
            vis[i]=true;
        }
    }
    int root=0;
    for(int i=1;i<=n;i++) {
        if(d[i]>n/2)root=i;
    }
    if(!root) {
        cout<<"Yes"<<"\n";
        for(int i=1;i<=m;i++) {
            if(vis[i]) {
                cout<<edge[i].u<<" "<<edge[i].v<<"\n";
            }
        }
        return;
    }

    dsu.fa[root]=root;
    dep[root]=0;
    for(auto [u,v,id]:vec[root]) {
        to[v]=id;
        dfs(v,root,v);
    }

    for(int i=1;i<=m;i++) {
        auto [u,v,id]=edge[i];
        if(u==root||v==root)continue;
        int fa_u=dsu.find(u),fa_v=dsu.find(v);
        if(fa_u==fa_v) continue;
        if(dep[u]<dep[v]) {
            swap(u,v);
            swap(fa_u,fa_v);
        }
        if(dep[v]==1&&d[u]>d[v]) {
            swap(u,v);
            swap(fa_u,fa_v);
        }
        ++d[u],++d[v];
        --d[root],--d[fa_v];
        vis[id]=1;
        vis[to[fa_v]]=0;
        dsu.fa[fa_v]=fa_u;
        if(d[root]<=n/2)break;
    }

    for(int i=1;i<=n;i++) {
        if(d[i]>n/2) {
            cout<<"No"<<"\n";
            return;
        }
    }

    cout<<"Yes"<<"\n";
    for(int i=1;i<=m;i++) {
        if(vis[i]) {
            cout<<edge[i].u<<" "<<edge[i].v<<"\n";
        }
    }
}

signed main() {
    become_faster();
    int T = 1;
    //T=read();
    cin>>T;
    //for(int i=1;i<=100;i++)solve(i);
    while (T--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9732kb

input:

2
6 9
1 2
1 3
1 4
2 3
2 4
3 4
4 5
4 6
4 6
3 4
1 3
2 3
3 3
1 2

output:

Yes
1 2
1 3
1 4
4 5
4 6
No

result:

ok 2 cases

Test #2:

score: -100
Runtime Error

input:

11140
10 15
9 6
5 7
6 5
2 3
7 5
7 5
3 10
9 7
5 5
9 1
7 5
2 8
7 5
4 3
6 2
9 19
3 7
3 9
2 8
2 8
3 6
5 1
1 8
8 9
8 3
4 8
5 5
3 1
4 3
1 3
8 6
1 3
7 4
4 3
8 8
12 20
10 2
5 5
2 4
3 3
3 3
5 11
9 2
5 5
7 12
11 3
3 3
3 5
5 3
3 1
4 6
7 11
6 8
4 5
6 12
6 5
8 18
4 2
4 3
2 4
2 4
4 3
4 8
2 2
6 7
2 4
6 2
1 4
8 7
4...

output:

Yes
9 6
5 7
6 5
2 3
3 10
9 1
2 8
4 3
6 2
Yes
3 7
3 9
2 8
3 6
5 1
1 8
8 9
4 8
Yes
10 2
2 4
5 11
9 2
7 12
11 3
3 1
4 6
7 11
6 8
4 5
Yes
4 2
4 3
4 8
6 7
6 2
1 4
7 5
Yes
6 5
5 7
5 9
4 3
2 9
2 3
8 7
5 1
Yes
10 2
2 6
3 2
1 9
8 10
4 6
6 1
2 5
1 7
Yes
5 7
5 4
7 1
2 6
6 7
1 3
Yes
12 3
1 13
7 8
8 2
10 6
1 6
1...

result: