| Task: | Building Teams |
| Sender: | aalto26bm_042 |
| Submission time: | 2026-09-07 17:00:05 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.01 s | details |
| #2 | ACCEPTED | 0.01 s | details |
| #3 | ACCEPTED | 0.01 s | details |
| #4 | ACCEPTED | 0.01 s | details |
| #5 | ACCEPTED | 0.01 s | details |
| #6 | ACCEPTED | 0.11 s | details |
| #7 | ACCEPTED | 0.11 s | details |
| #8 | ACCEPTED | 0.11 s | details |
| #9 | ACCEPTED | 0.11 s | details |
| #10 | ACCEPTED | 0.10 s | details |
| #11 | ACCEPTED | 0.01 s | details |
| #12 | ACCEPTED | 0.01 s | details |
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define die(x) return cout << x << endl, 0
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define FILE freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SZ(x) ((int)(x).size())
typedef long double ld;
typedef long long ll;
int gcd(int x,int y){ return (!y ? x : gcd(y, x%y)); }
int to_int(string sconvert){stringstream geek(sconvert);int xconvert = 0; geek >> xconvert; return xconvert;}
const int MAXN=200000+30,MAX_LOG=40,INF=1e18+10,MOD=1e9+7;
const double PI = acos(-1);
int mod(int x) { return (x % MOD + MOD) % MOD; }
int power(int x,int y){
if(y==0)return 1;
int c=power(x,y/2);
c=(c*c)%MOD;
if(y%2)c=(c*x)%MOD;
return c;
}
int n, t;
vector<int> g[MAXN];
int visited[MAXN];
int color[MAXN];
int max_color = 1;
int is_impossible = 0;
vector<pair<int,int>> edges;
int dfs(int v, int root){
visited[v]=1;
int ones = 0;
int twos = 0;
int is_leaf = 1;
for(int u:g[v]){
// cout <<v<<"pp pp"<<u<<" " <<visited[u]<<endl;
if(!visited[u]){
is_leaf = 0;
int c = dfs(u, root);
if (c == 1) ones++;
else twos ++;
}
else{
int c = color [u];
// cout <<c<<endl;
if (c != 0){
if (c == 1) ones++;
else twos ++;
}
}
}
if(is_leaf){
// cout<<v<<" kk"<<g[v].size()<<endl;
color[v] = 1;
return 1;
}
// cout<<ones<<" "<<twos<<"--- "<<v<<endl;
if (ones>0 && twos >0){
is_impossible=1;
return 1;
}
else if (ones>=0){
color[v]=2;
return 2;
}
else{
color[v]=1;
return 1;
}
}
int MAIN(){
int n, m;
cin>>n>>m;
for (int i = 0; i < m; ++i) {
int a, b;
cin >>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 1; i <= n; ++i) {
if (color[i] != 0) continue;
queue<int> q;
color[i] = 1;
q.push(i);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u : g[v]) {
if (color[u] == 0) {
color[u] = 3 - color[v];
q.push(u);
} else if (color[u] == color[v]) {
cout << "IMPOSSIBLE\n";
return 0;
}
}
}
}
for (int i = 1; i <= n; ++i) {
cout<<color[i] << (i == n ? '\n' : ' ');
}
return 0;
}
int32_t main(){
IOS;
int t=1;
// cin>>t;
while(t--)MAIN();
return 0;
}Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 10 20 3 4 8 10 3 7 1 8 ... |
| correct output |
|---|
| 1 1 1 2 2 1 2 2 2 1 |
| user output |
|---|
| 1 1 1 2 2 1 2 2 2 1 |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 10 20 1 3 8 10 2 4 6 8 ... |
| correct output |
|---|
| 1 1 2 2 1 1 1 2 1 1 |
| user output |
|---|
| 1 1 2 2 1 1 1 2 1 1 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 10 20 7 10 3 10 9 10 2 10 ... |
| correct output |
|---|
| 1 2 2 1 1 1 2 1 2 1 |
| user output |
|---|
| 1 2 2 1 1 1 2 1 2 1 |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 10 20 2 4 2 10 7 10 4 6 ... |
| correct output |
|---|
| 1 2 1 1 2 2 2 1 2 1 |
| user output |
|---|
| 1 2 1 1 2 2 2 1 2 1 |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 10 20 3 5 8 10 9 10 1 8 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 47355 96505 90709 92058 735 80715 91802 94265 ... |
| correct output |
|---|
| 1 2 2 1 2 1 1 1 2 2 1 2 1 1 1 ... |
| user output |
|---|
| 1 2 2 1 2 1 1 1 2 2 1 2 1 1 1 ... |
Test 7
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 59991 95794 95150 96051 78453 94730 90411 95523 ... |
| correct output |
|---|
| 1 1 1 2 2 1 1 2 1 2 1 2 2 2 1 ... |
| user output |
|---|
| 1 1 1 2 2 1 1 2 1 2 1 2 2 2 1 ... |
Test 8
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 89827 96402 65137 86792 80965 94708 19479 48078 ... |
| correct output |
|---|
| 1 2 1 1 2 1 2 2 2 1 2 1 1 2 1 ... |
| user output |
|---|
| 1 2 1 1 2 1 2 2 2 1 2 1 1 2 1 ... |
Test 9
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 72952 83723 66197 70052 2949 52160 55753 95651 ... |
| correct output |
|---|
| 1 1 2 2 2 1 1 2 2 2 2 2 1 2 1 ... |
| user output |
|---|
| 1 1 2 2 2 1 1 2 2 2 2 2 1 2 1 ... |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 38942 96755 70049 82663 7746 72732 87819 99029 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 11
Verdict: ACCEPTED
| input |
|---|
| 5 4 1 2 3 4 4 5 5 3 |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 4 5 1 2 1 4 2 3 2 4 ... |
| correct output |
|---|
| IMPOSSIBLE |
| user output |
|---|
| IMPOSSIBLE |
