제출 #431405

#제출 시각아이디문제언어결과실행 시간메모리
431405PbezzConnecting Supertrees (IOI20_supertrees)C++14
100 / 100
302 ms22208 KiB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
typedef pair<ll,ll> pii;

const ll MAXN = 2e5+5;
int parent[MAXN],sizee[MAXN],contagem[MAXN];
int parent1[MAXN],sizee1[MAXN];

int find(ll x){
if(x==parent[x])return x;

return parent[x]=find(parent[x]);
}

void unite(ll a, ll b){
a = find(a);
b = find(b);
if(a==b)return;
if(sizee[a]<sizee[b])swap(a,b);

sizee[a]+=sizee[b];
parent[b]=a;
}

int find1(ll x){
if(x==parent1[x])return x;

return parent1[x]=find1(parent1[x]);
}

void unite1(ll a, ll b){
a = find1(a);
b = find1(b);
if(a==b)return;
if(sizee1[a]<sizee1[b])swap(a,b);

sizee1[a]+=sizee1[b];
parent1[b]=a;
}

int construct(std::vector<std::vector<int>> p) {
	int n = p.size(),i,j,k,a,b;
	std::vector<std::vector<int>> answer;
	for (i = 0; i < n; i++) {
		std::vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}

	for(i=0;i<n;i++){
	parent[i]=i;
	sizee[i]=1;
	parent1[i]=i;
	sizee1[i]=1;
	}

	for(i=0;i<n;i++){
	for(j=i+1;j<n;j++){
	if(p[i][j]==0)continue;

	if(p[i][j]==1)contagem[i]++;

	if(p[i][j]==1)contagem[j]++;

	unite(i,j);
	}
}
vector<vector<int>>meh(n);
	for(i=0;i<n;i++){
	k=find(i);
	meh[k].pb(i);
	}//meh[i] e set da componente de i

	for(a=0;a<n;a++){
	if(a!=find(a))continue;//para cada componente
	k = meh[a].size();
	for(i=0;i<k;i++){
	for(j=i+1;j<k;j++){	if(p[ meh[a][i] ][ meh[a][j] ]==0)return 0;
	if(p[ meh[a][i] ][ meh[a][j] ]==2)continue;

	unite1(meh[a][i],meh[a][j]);
	}
}//componentes feitas

vector<vector<int>>meh1(n);
	for(i=0;i<k;i++){
	b=find1(meh[a][i]);//cout<<i<<" "<<meh[a][i]<<" "<<b<<endl;
	meh1[b].pb(meh[a][i]);
	}
	vector<int>fina;
	for(i=0;i<k;i++){
	if(find1(meh[a][i])!=meh[a][i])continue;
//meh[a][i] e representante de linha
	//cout<<meh[a][i]<<" "<<(int)meh1[ meh[a][i] ].size()<<"hicbnaui\n";
	for(j=1;j<(int)meh1[ meh[a][i] ].size();j++){//fazer linha
	answer[ meh1[ meh[a][i] ][j] ][ meh1[ meh[a][i] ][j-1] ]=1;
	answer[ meh1[ meh[a][i] ][j-1] ][ meh1[ meh[a][i] ][j] ]=1;

for(b=0;b<(int)meh1[ meh[a][i] ].size();b++){
if(p[ meh1[ meh[a][i] ][b] ][ meh1[ meh[a][i] ][j] ]==2)return 0;
}

	//ver se nao tem de ter 1 para algum de fora
	if(contagem[ meh1[ meh[a][i] ][j] ]!=(int)meh1[ meh[a][i] ].size()-1){
	//cout<<"hihihih "<<meh1[ meh[a][i] ][j]<<" "<<contagem[ meh1[ meh[a][i] ][j] ]<<endl;  ;
	return 0;
	}
	}

	fina.pb(meh[a][i]);
	}

	for(j=1;j<(int)fina.size();j++){
	answer[ fina[j] ][ fina[j-1] ]=1;
	answer[ fina[j-1] ][ fina[j] ]=1;
	}
	if(fina.size()>1){
	answer[ fina[(int)fina.size()-1] ][ fina[0] ]=1;
	answer[ fina[0] ][ fina[(int)fina.size()-1] ]=1;
	}
	if(fina.size()==2)return 0;
	}



	build(answer);
	return 1;
}
#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...