Submission #1300027

#TimeUsernameProblemLanguageResultExecution timeMemory
1300027opeleklanosConnecting Supertrees (IOI20_supertrees)C++20
19 / 100
103 ms22224 KiB
#include <iostream>
#include <vector>
#include "supertrees.h"
using namespace std;



int n;

vector<int> sz;
vector<int> par;

int find(int st){
    if( st != par[st] ) return par[st] = find(par[st]);
    return st;
}

void connect(int a, int b){
    a = find(a);
    b = find(b);
    if(a == b) return;
    if(sz[a] < sz[b]) swap(a, b);
    par[b] = a;
    sz[a] += sz[b];
    
}


int construct(vector<vector<int>> p){
    n = p.size();
    sz.assign(n, 1);
    par.assign(n, 0);
    for(int i = 0; i<n; i++) par[i] = i;
    for(int i = 0; i<n; i++){
        for(int j = 0; j<n; j++){
            if(p[i][j]) connect(i, j);
        }
    }
    for(int i = 0; i<n; i++){
        for(int j = 0; j<n; j++){
            if((p[i][j] == 0) && (find(i) == find(j))){
                //cout<<-1<<endl;
                return 0;
            }
            if((p[i][j] != 0) && (find(i) != find(j))){
                return 0;
            }
            if(i == j && p[i][j] == 2) return 0;
            if(p[i][j] != p[j][i]) return 0;
        }
    }
    vector<vector<int>> groups(n);
    vector<vector<int>> ans;
    ans.assign(n, vector<int>(n, 0));
    for(int i = 0; i<n; i++){
        groups[find(i)].push_back(i);
    }

    for(int i = 0; i<n; i++){
        if(groups[i].size() > 2){
            for(int j = 0; j<groups[i].size(); j++){
                if(j>0)
                {ans[groups[i][j]][groups[i][j-1]] = ans[groups[i][j-1]][groups[i][j]] = 1;}

                if(j<groups[i].size()-1) 
                {ans[groups[i][j]][groups[i][j+1]] = ans[groups[i][j+1]][groups[i][j]] = 1;}
            }
            ans[groups[i][0]][groups[i][groups[i].size()-1]] = 
            ans[groups[i][groups[i].size()-1]][groups[i][0]] = 1;
        }
        if(groups[i].size() == 2) return 0;
    }

    build(ans);

    // for(int i = 0; i<n; i++){
    //     for(int j = 0; j<n; j++){
    //         cout<<ans[i][j]<<' ';
    //     }
    //     cout<<endl;
    // }
    return 1;
}

// int main(void){
//     freopen("input.txt", "r", stdin);
//     int t; cin>>t;
//     vector<vector<int>> tmp(t, vector<int>(t, 0));
//     for(int i = 0; i<t; i++) for(int j = 0; j<t; j++) cin>>tmp[i][j];
//     construct(tmp);
// }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...