# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
653743 | Litusiano | Connecting Supertrees (IOI20_supertrees) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "supertrees.h"
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define f first
#define s second
#define ii pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define vvii vector<vector<ii>>
#define pb push_back
#define vpi vector<ii>
#define forcin for(int i = 0; i<n; ++i) cin>>v[i];
#define pq priority_queue<ii>
#define mp make_pair
#define ld long double
#define vc vector<char>
#define vvc vector<vc>
#define vb vector<bool>
#define vvb vector<vb>
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define For(i, n, x) for (int i = x; i < n; i++)
#define rsz(a,x) assign(a,x)
#define endl "\n"
vvi G;
vb used1;
vi vis;
void dfs(int u, int p){
used1[u] = 1;
for(int i : G[u]){
if(i != p) vis[i]++;
if(!used1[i]) dfs(i,u);
}
}
int construct(vvi p){
// Cap cami, components diferents
// tots == 1, graf lineal
// tots == 2, graf lineal amb el ultim unit al primer
int N = p.size();
bool ok = 0;
For(i,N,0){
ok|=(p[i][i] == 0);
}
For(i,N,0){
For(j,N,0){
ok|=(p[i][j] != p[j][i]);
}
}
if(ok) return 0;
vvi used(N,vi(N,0));
/*if(N == 1){
if(p[0][0] == 1){
build(used); return 1;
}
return 0;
}*/
vpi edges;
/*For(i,N,0){
For(j,N,0) cout<<p[i][j]<<" "; cout<<endl;
}*/
for(auto x : p){
for(auto e : x){
if(e == 3) return 0;
}
}
For(i,N,0){
For(j,N,0){
if(used[i][j] || i == j) continue;
//cout<<p[i][j]<<" "<<j<<endl;
if(p[i][j] == 0) continue;
//cout<<i<<" "<<j<<endl;
// si tinc 1, vaig buscant 1ns sino dosos
int cur = j;
set<int> act;
edges.pb({i,j});
act.insert(i);
used[i][j] = used[j][i] = 1;
// Miro TOT EL CICLE
int last = j;
int prev = i;
while(!act.count(cur)){
act.insert(j);
bool b = false;
int nextcur = i;
For(k,N,0){
//if(cur == j && k == i) continue;
if(cur == k) continue;
if(cur == 2){
//cout<<"H"<<" "<<k<<" "<<act.count(k)<<" "<<used[cur][k]<<endl;
//for(auto x : act) cout<<x<<" "; cout<<"ENDH"<<endl;
}
if(act.count(k)){
if(p[cur][k] != p[i][j]){
//cout<<"HERE "<<cur<<" "<<k<<" "<<i<<" "<<j<<" "<<p[i][j]<<" "<<p[cur][k]<<endl;
return 0;
}
}
else if(!act.count(k) && p[cur][k] == p[i][j] && !used[cur][k] && !b){
//cout<<i<<" "<<j<<" CUR "<<cur<<" "<<k<<endl;
//for(auto x : act) cout<<x<<" "; cout<<endl;
//act.insert(k);
for(auto x : act){
used[x][k] = used[k][x] = 1;
// Miro si es valid
if(p[x][k] != p[cur][k]) return 0;
}
b = 1;
//afegir edge
edges.pb({cur,k});
used[cur][k] = 1;
used[k][cur] = 1;
last = cur;
nextcur = k;
}
}
act.insert(cur);
last = cur;
cur = nextcur;
}
//cout<<i<<" "<<j<<endl;
//for(auto x : act) cout<<x<<" "; cout<<endl;
//cerr<<last<<" "<<i<<endl;
bool bo = 0;
if(act.size() > 1){
int num = p[i][j];
for(auto x : act){
for(auto e : act){
if(p[e][x] != num) bo = 1;
}
}
}
if(p[i][j] == 2) edges.pb({last,i});
if(bo) return 0;
if(act.size() == 2 && p[i][j] == 2 ){
//cerr<<i<<" "<<j<<" "<<endl;
//for(auto x : act) cerr<<x<<" "; cerr<<endl;
return 0;
}
}
}
//cout<<"AAAAA"<<endl;
//cerr<<edges.size()<<endl;
vvi ans(N,vi(N,0));
if(!edges.size()){
// 1 0 1 0 1 0 1 0 1
For(i,N,0){
For(j,N,0){
if(p[i][j] == 1 && i != j) return 0;
if(p[i][j] == 2) return 0;
}
}
build(ans); return 1;
}
sort(all(edges));
G.rsz(N,vi());
for(auto x : edges){
G[x.f].pb(x.s); G[x.s].pb(x.f);
}
for(auto x : edges){
//if(ans[x.f][x.s]) return 0;
ans[x.f][x.s] = ans[x.s][x.f] = 1;
//cerr<<x.f<<" "<<x.s<<endl;
}
For(i,n,0){
used1.rsz(N,false); vis.rsz(N,0);
dfs(i,-1);
vis[i] = max(vis[i],1);
For(j,n,0){
//cerr<<i<<" "<<j<<" "<<vis[j]<<" "<<p[i][j]<<endl;
if(vis[j] != p[i][j]) return 0;
}
}
build(ans);
return 1;
}