QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#288432 | #5655. Train Splitting | Sorting# | WA | 1ms | 3448kb | C++14 | 1.4kb | 2023-12-22 17:17:03 | 2023-12-22 17:17:03 |
Judging History
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)