QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#356094#6421. Degree of Spanning TreePorNPtreeTL 0ms15016kbC++143.4kb2024-03-17 15:30:222024-03-17 15:30:22

Judging History

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

  • [2024-03-17 15:30:22]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:15016kb
  • [2024-03-17 15:30:22]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

vector<int> G[N], tG[N];
vector< pair<int, int> > T, E;
map< pair<int, int>, int> M;
mt19937 rnd;
int du[N];

void dfs(int x, int fa = -1)
{
    shuffle(G[x].begin(), G[x].end(), rnd);
    for (int i = 0; i < (int)G[x].size(); ++i) {
        int v = G[x][i];
        if (!du[v]) {
            T.push_back(make_pair(min(x, v), max(x, v)));
            tG[x].push_back(v);
            tG[v].push_back(x);
            ++du[x], ++du[v];
            dfs(v, x);
        } else if (v != fa) {
            if (!M.count(make_pair(min(x, v), max(x, v)))) {
                E.push_back(make_pair(min(x, v), max(x, v)));
                M[make_pair(min(x, v), max(x, v))] = 1;
            }
        }
    }
    return;
}

int col[N];

void color(int x, int c, int fa)
{
    col[x] = c;
    for (int i = 0; i < (int)tG[x].size(); ++i) {
        int v = tG[x][i];
        if (v != fa) {
            color(v, c, x);
        }
    }
    return;
}

int fa[N];

int find(int x)
{
    return (fa[x] == x ? x : fa[x] = find(fa[x]));
}

signed main()
{
    int TT;
    scanf("%d", &TT);
    while (TT--) {
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; ++i) {
            G[i].clear();
            tG[i].clear();
            du[i] = 0;
        }
        M.clear();
        while (m--) {
            int x, y;
            scanf("%d%d", &x, &y);
            if (x != y && !M.count(make_pair(min(x, y), max(x, y)))) {
                M[make_pair(min(x, y), max(x, y))] = 1;
                G[x].push_back(y);
                G[y].push_back(x);
            }
        }
        if (n == 2) {
            puts("Yes");
            puts("1 2");
            continue;
        }
        if (n == 3) {
            puts("No");
            continue;
        }
        M.clear();
        dfs(1);
        int flag = 0;
        for (int i = 1; i <= n; ++i) {
            if (du[i] > n / 2) {
                flag = i;
                break;
            }
        }
        if (!flag) {
            puts("Yes");
            for (int i = 0; i < (int)T.size(); ++i) {
                printf("%d %d\n", T[i].first, T[i].second);
            }
            continue;
        }
        col[flag] = 0;
        for (int i = 0; i < (int)tG[flag].size(); ++i) {
            color(tG[flag][i], i + 1, flag);
            fa[i + 1] = i + 1;
        }
        set< pair<int, int> > res;
        for (int i = 0; i < (int)T.size(); ++i) {
            res.insert(T[i]);
        }
        for (int i = 0; i < (int)E.size() && du[flag] > n / 2; ++i) {
            int x = E[i].first, y = E[i].second;
            if (x == flag || y == flag || find(x) == find(y)) {
                continue;
            }
            res.insert(E[i]);
            ++du[x], ++du[y];
            int c1 = tG[flag][find(x) - 1], c2 = tG[flag][find(y) - 1], wh = du[c1] > du[c2] ? c1 : c2;
            res.erase(make_pair(min(flag, wh), max(flag, wh)));
            --du[flag], --du[wh];
            fa[find(x)] = find(y);
        }
        if (du[flag] <= n / 2) {
            puts("Yes");
            for (set< pair<int, int> >::iterator it = res.begin(); it != res.end(); ++it) {
                printf("%d %d\n", (*it).first, (*it).second);
            }
        } else {
            puts("No");
        }
    }
    return 0;
}

詳細信息

Test #1:

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

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
2 4
4 5
4 6
No

result:

ok 2 cases

Test #2:

score: -100
Time Limit Exceeded

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
1 9
6 9
2 6
2 3
3 10
3 4
2 8
5 6
5 7
Yes
1 9
6 9
2 6
2 3
3 10
3 4
2 8
5 6
5 7
1 5
1 8
4 8
4 7
3 7
3 6
3 9
2 8
Yes
1 9
6 9
2 6
2 3
3 10
3 4
2 8
5 6
5 7
1 5
1 8
4 8
4 7
3 7
3 6
3 9
2 8
1 3
3 11
5 11
4 5
2 4
2 10
2 9
4 6
6 8
6 12
7 12
Yes
1 9
6 9
2 6
2 3
3 10
3 4
2 8
5 6
5 7
1 5
1 8
4 8
4 7
3 7
3 6...

result: