제출 #312916

#제출 시각아이디문제언어결과실행 시간메모리
312916CodeKracker슈퍼트리 잇기 (IOI20_supertrees)C++14
19 / 100
268 ms26168 KiB
/*input

*/
/**
    Author: Kristopher Paul
    Date Created: 30-09-2020
**/
#include<bits/stdc++.h>
#include<stdio.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
//#define int ll
#define pb push_back
#define INF 1e18
//#define MOD 1000000007
#define MOD 998244353
#define mp make_pair
const double PI=3.141592653589793238462643383279502884197169399375105820974944;
#define REP(i,n) for (int i = 0; i < n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REPD(i,n) for (int i = n-1; i >= 0; i--)
#define FORD(i,a,b) for (int i = a; i >= b; i--)
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define umap map
#define pii pair<int,int>
#define F first
#define S second
#define mii map<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define itr :: iterator it
#define all(v) v.begin(),v.end()
#define WL(t) while(t--)
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define out(x) cout << #x << " is " << x << endl
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;

//Uncomment for File I/O
//ifstream fin("input.in")

//using namespace __gnu_pbds;

//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds; // set
//typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> pbds; // multiset

int ModExp(int x,int y,int m){
    int res = 1;
    x = x % m;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % m;

        y = y>>1;
        x = (x*x) % m;
    }
    return res;
}

void build(vector<vi> b);

int bpar[1005];
int bran[1005];
int cpar[1005];
int cran[1005];

int bfpar(int x){
    if(x == bpar[x]){
        return x;
    }
    return bpar[x] = bfpar(bpar[x]);
}

void buni(int u,int v){
    int a = bfpar(u);
    int b = bfpar(v);
    if(a == b){
        return;
    }
    if(bran[a] < bran[b]){
        bpar[a] = b;
    }else if(bran[b] < bran[a]){
        bpar[b] = a;
    }else{
        bpar[a] = b;
        bran[b]++;
    }
}

int cfpar(int x){
    if(x == cpar[x]){
        return x;
    }
    return cpar[x] = cfpar(cpar[x]);
}

void cuni(int u,int v){
    int a = cfpar(u);
    int b = cfpar(v);
    if(a == b){
        return;
    }
    if(cran[a] < cran[b]){
        cpar[a] = b;
    }else if(cran[b] < cran[a]){
        cpar[b] = a;
    }else{
        cpar[a] = b;
        cran[b]++;
    }
}

void rec(){
    rec();
}

int construct(vector<vi> p){
    int n = p.size();
    bool isb[n] = {};
    bool isc[n] = {};
    FOR(i,0,n){
        bpar[i] = i;
        bran[i] = 1;
        cpar[i] = i;
        cran[i] = 1;
    }
    vector<vi> ans(n,vi(n,0));
    FOR(i,0,n){
        FOR(j,0,n){
            if(p[i][j] == 2){
                cuni(i,j);
                isc[i] = isc[j] = true;
            }
            if(p[i][j] == 1 && i != j){
                buni(i,j);
                isb[i] = isb[j] = true;
            }
        }
    }
    bool cvis[n] = {};
    vector<vi> conp(n,vi(n,0));
    FOR(i,0,n){
        if(!cvis[cpar[i]]){
            cvis[cpar[i]] = true;
            vi cycle;
            set<int> tmp;
            FOR(j,0,n){
                if(cpar[j] == cpar[i]){
                    cycle.pb(j);
                }
            }
            int sz = cycle.size();
            if(sz == 2){
                return 0;
            }
            vi comp;
            set<int> scomp;
            /*if(sz == 1){
                conp[cycle[0]][cycle[0]] = 1;
                continue;
            }*/
            vi special;
            FOR(ind,0,cycle.size()){
                if(ind != 0){
                    ans[cycle[ind]][cycle[ind-1]] = 1;
                }else if(sz > 1){
                    ans[cycle[ind]][cycle[sz-1]] = 1;
                }
                if(ind != (sz-1)){
                    ans[cycle[ind]][cycle[ind+1]] = 1;
                }else if(sz > 1){
                    ans[cycle[ind]][cycle[0]] = 1;
                }
                FOR(iind,0,sz){
                    if(ind == iind){
                        conp[cycle[ind]][cycle[iind]] = 1;
                        continue;
                    }
                    conp[cycle[ind]][cycle[iind]] = 2;
                }
                if(isb[cycle[ind]]){
                    special.pb(cycle[ind]);
                    FOR(j,0,n){
                        if(bpar[j] == bpar[cycle[ind]]){
                            comp.pb(j);
                            scomp.insert(j);
                        }
                    }
                }else{
                    comp.pb(cycle[ind]);
                    scomp.insert(cycle[ind]);
                }
            }
            if(comp.size() != scomp.size()){
                return 0;
            }
            FOR(ind,0,special.size()){
                int internode = special[ind];
                int curnode = internode;
                vi branch_nodes;
                branch_nodes.pb(internode);
                FOR(iind,0,comp.size()){
                    if(bpar[internode] == bpar[comp[iind]] && comp[iind] != internode){
                        ans[curnode][comp[iind]] = 1;
                        ans[comp[iind]][curnode] = 1;
                        curnode = comp[iind];
                        branch_nodes.pb(curnode);
                    }
                }
                FOR(iind,0,branch_nodes.size()){
                    int branch_node = branch_nodes[iind];
                    FOR(iiind,0,comp.size()){
                        if(bpar[branch_node] == bpar[comp[iiind]]){
                            conp[branch_node][comp[iiind]] = 1;
                            conp[comp[iiind]][branch_node] = 1;
                        }else{
                            conp[branch_node][comp[iiind]] = 2;
                            conp[comp[iiind]][branch_node] = 2;
                        }
                    }
                }
            }
        }
    }

    /*FOR(i,0,n){
        FOR(j,0,n){
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }*/
    if(p != conp){
        //rec();
        return 0;
    }
    build(ans);
    return 1;
}

컴파일 시 표준 에러 (stderr) 메시지

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:21:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 | #define FOR(i,a,b) for (int i = a; i < b; i++)
......
  167 |             FOR(ind,0,cycle.size()){
      |                 ~~~~~~~~~~~~~~~~~~    
supertrees.cpp:167:13: note: in expansion of macro 'FOR'
  167 |             FOR(ind,0,cycle.size()){
      |             ^~~
supertrees.cpp:21:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 | #define FOR(i,a,b) for (int i = a; i < b; i++)
......
  201 |             FOR(ind,0,special.size()){
      |                 ~~~~~~~~~~~~~~~~~~~~  
supertrees.cpp:201:13: note: in expansion of macro 'FOR'
  201 |             FOR(ind,0,special.size()){
      |             ^~~
supertrees.cpp:21:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 | #define FOR(i,a,b) for (int i = a; i < b; i++)
......
  206 |                 FOR(iind,0,comp.size()){
      |                     ~~~~~~~~~~~~~~~~~~
supertrees.cpp:206:17: note: in expansion of macro 'FOR'
  206 |                 FOR(iind,0,comp.size()){
      |                 ^~~
supertrees.cpp:21:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 | #define FOR(i,a,b) for (int i = a; i < b; i++)
......
  214 |                 FOR(iind,0,branch_nodes.size()){
      |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:214:17: note: in expansion of macro 'FOR'
  214 |                 FOR(iind,0,branch_nodes.size()){
      |                 ^~~
supertrees.cpp:21:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 | #define FOR(i,a,b) for (int i = a; i < b; i++)
......
  216 |                     FOR(iiind,0,comp.size()){
      |                         ~~~~~~~~~~~~~~~~~~~
supertrees.cpp:216:21: note: in expansion of macro 'FOR'
  216 |                     FOR(iiind,0,comp.size()){
      |                     ^~~
#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...