QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#135236#6634. Central Subset4k2kok#WA 8ms16280kbC++203.1kb2023-08-05 13:07:442023-08-05 13:07:47

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-05 13:07:47]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:16280kb
  • [2023-08-05 13:07:44]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a,b) memset((a),(b),sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
#define int long long
#define db long double

const double eps = 1e-6;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
int n, m, d[maxn], vis[maxn], fa[maxn];
vector<int> dep[maxn];
vector<int> edge[maxn];

void bfs(int u) {
    queue<int> que;
    que.push(u);
    vis[u] = 1;
    fa[u] = -1;
    dep[1].push_back(u);
    while(!que.empty()) {
        int now = que.front();
        que.pop();
        for(int i = 0; i < edge[now].size(); i++) {
            int v = edge[now][i];
            if(vis[v] == 0) {
                d[now]++; d[v]++;
                vis[v] = vis[now] + 1;
                fa[v] = now;
                dep[vis[v]].push_back(v);
                que.push(v);
            }
        }
    }
}

void solve() {
    cin >> n >> m;
    if(m == 0) {
        cout << 1 << '\n';
        cout << 1 << '\n';
        return;
    }
    for(int i = 0; i <= n; i++) d[i] = 0, vis[i] = 0, edge[i].clear(), dep[i].clear();
    int pos = 0, maxx = 0;
    for(int i = 0, u, v; i < m; i++) {
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
        d[u]++; d[v]++;
        if(d[u] > maxx) {
            maxx = d[u];
            pos = u;
        }
        if(d[v] > maxx) {
            maxx = d[v];
            pos = v;
        }
    }
    for(int i = 0; i <= n; i++) d[i] = 0;
    bfs(pos);
    // cerr << pos << '\n';
    // for(int i = 1; i <= n; i++) {
    //     cerr << i << ": " << vis[i] << '\n';
    // }
    int len = (int)ceil(sqrt(n));
    set<int> ans, temp;
    for(int beg = 1; beg <= 1 + len && dep[beg].size() > 0; beg++) {
        for(int i = beg; dep[i].size() > 0; i += (2 * len + 1)) {
            for(int j = 0; j < dep[i].size(); j++) {
                temp.insert(dep[i][j]);
            }
        }
        // cerr << temp.size() << '\n';
        if(ans.empty()) ans = temp;
        else if(temp.size() <= ans.size()) ans = temp;
        temp.clear();
    }   
    for(int i = 1; i <= n; i++) {
        if(d[i] == 1) {
            int u = i;
            int cnt = 0, flag = 1;
            while(u != -1) {
                if(ans.count(u)) {
                    break;
                }
                cnt++;
                if(cnt >= len + 1) {
                    ans.insert(i);
                    flag = 0;
                    break;
                } 
                u = fa[u];
            }
            if(u == -1 && flag) ans.insert(i);
        }
    }
    if(ans.size() > len) {
        cout << -1 << '\n';
    }
    else {
        cout << ans.size() << '\n';
        for(auto it : ans) {
            cout << it << ' ';
        }
        cout << '\n';
    }
}

signed main(){
    io;
        int T = 1;
        cin >> T;
        while(T--) {
            solve();
        }
        return 0;
}

詳細信息

Test #1:

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

input:

2
4 3
1 2
2 3
3 4
6 7
1 2
2 3
3 1
1 4
4 5
5 6
6 4

output:

2
1 4 
1
1 

result:

ok correct (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 8ms
memory: 16244kb

input:

10000
15 14
13 12
5 4
9 8
11 12
15 14
10 9
14 13
2 3
2 1
6 5
10 11
3 4
7 6
8 7
6 5
2 1
2 4
4 6
2 3
3 5
10 9
8 3
9 4
5 6
5 10
3 2
5 4
2 7
1 2
4 3
2 1
2 1
2 1
2 1
9 8
9 8
5 4
1 2
6 5
3 4
3 2
7 8
7 6
2 1
1 2
14 13
3 10
5 6
2 9
11 4
2 3
2 1
8 7
13 6
5 4
5 12
6 7
4 3
7 14
16 15
2 3
2 1
6 10
6 9
6 4
9 11
...

output:

3
1 8 15 
2
2 5 
1
5 
2
1 2 
2
1 2 
3
1 5 9 
2
1 2 
4
2 8 13 14 
2
6 16 
1
2 
3
2 12 20 
1
4 
1
5 
1
10 
1
2 
3
1 8 15 
2
2 11 
2
1 2 
1
4 
1
2 
2
1 4 
-1
4
2 8 13 14 
5
3 13 15 16 18 
2
2 12 
3
1 9 15 
2
2 12 
3
1 6 10 
2
1 3 
1
2 
3
1 9 20 
1
2 
1
5 
2
6 16 
1
2 
2
1 6 
1
3 
3
1 6 8 
3
5 20 21 
1
...

result:

wrong answer Integer 11 violates the range [1, 7] (test case 17)