# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
779164 | 2023-07-11T08:35:44 Z | jasmin | 슈퍼트리 잇기 (IOI20_supertrees) | C++17 | 0 ms | 0 KB |
#include "supertrees.h" #include<bits/stdc++.h> using namespace std; /*void build(vector<vector<int> > adi){ int n=adi.size(); for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ cout << adi[i][j] << " "; } cout << "\n"; } }*/ vector<vector<int> > components(vector<vector<int> >& p, int n, bool& pos){ vector<vector<int> > comp; vector<bool> vis(n); for(int v=0; v<n; v++){ if(vis[v]) continue; vis[v]=true; comp.push_back({}); comp.back().push_back(v); for(int u=0; u<n; u++){ if(u!=v && p[v][u]!=0){ comp.back().push_back(u); if(vis[u] || p[u][v]==0) pos=false; vis[u]=true; } } } return comp; } void solve_component(vector<int>& c, int n, vector<vector<int> >& p, vector<vector<int> >& adi, bool& pos){ vector<int> cycle; vector<bool> vis(n); for(auto v: c){ if(vis[v]) continue; cycle.push_back(v); for(auto u: c){ if(u!=v && p[v][u]==1){ adi[v][u]=1; adi[u][v]=1; vis[u]=true; for(int i=0; i<n; i++){ pos = pos && (p[v][i] == p[u][i]); } } } } for(int i=0; i+1<(int)cycle.size(); i++){ adi[cycle[i]][cycle[i+1]]=1; adi[cycle[i+1]][cycle[i]]=1; } adi[cycle[0]][cycle.back()]=1; adi[cycle.back()][cycle[0]]=1; } int construct(vector<vector<int> > p) { int n = p.size(); bool pos=true; for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(p[i][j]==3){ pos=false; } } } vector<vector<int> > comp=components(p, n, pos); /*cout << "components\n"; for(auto e: comp){ for(auto x: e) cout << x << " "; cout << "\n"; }*/ for(auto c: comp){ solve_component(c, n, p, adi, pos); } if(pos){ build(adi); return 1; } return 0; } /*signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<vector<int> > p(n, vector<int> (n)); for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ cin >> p[i][j]; } } cout << construct(p) << "\n"; }*/