Submission #986612

#TimeUsernameProblemLanguageResultExecution timeMemory
986612aaaaaarrozConnecting Supertrees (IOI20_supertrees)C++17
100 / 100
178 ms38152 KiB
    #include "supertrees.h"
    #include<bits/stdc++.h>
    using namespace std;
    #define sz(a) (int)(a.size())
    int p[1111][1111],q[1111][1111],ans[1111][1111];
    void add(int v,int u){
    	ans[v][u] = ans[u][v] = 1;
    }
    int construct(vector<vector<int>> P) {
    	int n = sz(P);
    	for (int i=0;i<n;i++){
    		if(P[i][i] != 1)return 0;
    		for(int j=0;j<n;j++){
    			q[i][j] = ans[i][j] = 0;
    			p[i][j] = P[i][j];
    			if(p[i][j] == 3)return 0;
    		}
    	}
    	//find all nodes connected to 1
    	//then nodes with p(1,v) = 1
    	//this has to form a tree
    	//repeat forming many trees then link the trees in a cycle
    	vector<int>ded(n,0);
    	for(int v=0;v<n;v++){
    		if(ded[v])continue;
    		vector<int>comp;
    		vector<bool>incomp(n,0);
    		for(int j=0;j<n;j++){
    			if(ded[j])continue;
    			if(p[v][j]){
    				incomp[j] = 1;
    				comp.push_back(j);
    				ded[j] = 1;
    			}
    		}
    		for(int x:comp){
    			for(int y:comp){
    				if(!p[x][y])return 0;
    			}
    		}
    		vector<vector<int>>trees;
    		while(!comp.empty()){
    			int v = comp[0];
    			vector<int>tree,tmp;
    			for(int u:comp){
    				if(p[u][v] == 1)tree.push_back(u);
    				else tmp.push_back(u);
    			}
    			trees.push_back(tree);
    			comp = tmp;
    		}
    		int ss = sz(trees);
    		if(ss==2)return 0;
    		if(ss > 1)add(trees[0][0],trees[ss-1][0]);
    		for(int i=0;i<ss;i++){
    			if(i+1<sz(trees))add(trees[i][0],trees[i+1][0]);
    			for(int j=0;j+1<sz(trees[i]);j++)add(trees[i][j],trees[i][j+1]);
     
    			vector<bool>intree(n,0);
    			for(int x:trees[i])intree[x] = 1;
    			for(int x:trees[i]){
    				for(int y:trees[i])q[x][y] = 1;
    				for(int j=0;j<n;j++){
    					if(incomp[j] && !intree[j])q[x][j] = q[j][x] = 2;
    				}
    			}
    		}
    	}
    	for(int i=0;i<n;i++){
    		for(int j=0;j<n;j++){
    			if(q[i][j] != p[i][j])return 0;
    		}
    	}
    	vector<vector<int>>fin(n);
    	for(int i=0;i<n;i++){
    		fin[i].assign(n,0);
    		for(int j=0;j<n;j++)fin[i][j] = ans[i][j];
    	}
    	build(fin);
    	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...