Submission details
Task:Building Teams
Sender:aalto26bm_042
Submission time:2026-09-07 16:54:18 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#10.01 sdetails
#20.01 sdetails
#30.01 sdetails
#40.01 sdetails
#5ACCEPTED0.01 sdetails
#60.12 sdetails
#70.12 sdetails
#80.12 sdetails
#90.12 sdetails
#10ACCEPTED0.12 sdetails
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.01 sdetails

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 u, v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i=1;i<=n;i++){
        if(!visited[i]){
            dfs(i, i);            
        }
    }
    if(is_impossible){
        cout<<"IMPOSSIBLE";
        return 0;
    }
    for (int i = 1; i <= n; i++){
        cout<<color[i]<<" ";
    }
    
    return 0;
}
int32_t main(){
    IOS;
    int t=1;
    // cin>>t;
    while(t--)MAIN();
    return 0;
}

Test details

Test 1

Verdict:

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
IMPOSSIBLE

Test 2

Verdict:

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
IMPOSSIBLE

Test 3

Verdict:

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
IMPOSSIBLE

Test 4

Verdict:

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
IMPOSSIBLE

Test 5

Verdict: ACCEPTED

input
10 20
3 5
8 10
9 10
1 8
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 6

Verdict:

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
IMPOSSIBLE

Test 7

Verdict:

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
IMPOSSIBLE

Test 8

Verdict:

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
IMPOSSIBLE

Test 9

Verdict:

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
IMPOSSIBLE

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