QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#288432#5655. Train SplittingSorting#WA 1ms3448kbC++141.4kb2023-12-22 17:17:032023-12-22 17:17:03

Judging History

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

  • [2023-12-22 17:17:03]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3448kb
  • [2023-12-22 17:17:03]
  • 提交

answer

#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
#include <functional>

using namespace std;
typedef long long ll;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;

    while(t--){
        int n, m;
        cin >> n >> m;

        vector<int> sz(n + 1), par(n + 1);
        for(int i = 1; i <= n; ++i){
            sz[i] = 1;
            par[i] = i;
        }

        function<int(int)> get_par = [&](int x){
            if(par[x] == x) return x;
            return par[x] = get_par(par[x]);
        };

        auto unite = [&](int a, int b){
            a = get_par(a), b = get_par(b);
            if(a == b) return false;
            if(sz[a] < sz[b]) swap(a, b);
            sz[a] += sz[b];
            par[b] = a;
            return true;
        };

        int cnt_e = 0, cnt_c = 2;
        vector<int> ans;
        for(int i = 0; i < m; ++i){
            int u, v;
            cin >> u >> v;

            if(unite(u, v)){
                if(cnt_e & 1){
                    ans.push_back(1);
                }
                else{
                    ans.push_back(2);
                }
                ++cnt_e;
            }
            else{
                ans.push_back(++cnt_c);
            }
        }

        cout << cnt_c << "\n";
        for(int x: ans)
            cout << x << " ";
        cout << "\n";
    }
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3448kb

input:

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

output:

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

result:

wrong answer colors 1 and 3 are not connected (test case 1)