답안 #842014

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
842014 2023-09-02T10:10:54 Z 8pete8 슈퍼트리 잇기 (IOI20_supertrees) C++17
0 / 100
1 ms 604 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
#include "supertrees.h"
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define pb push_back
#define all(x) x.begin(),x.end()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
const int mxn=1e3,mod=998244353,lg=42,root=80,inf=1e9;
void setIO(string name) {
	ios_base::sync_with_stdio(0); cin.tie(0);
	freopen((name+".in").c_str(),"r",stdin);
	freopen((name+".out").c_str(),"w",stdout);
}
int last[mxn+10],first[mxn+10];
struct dsu{
    int pa[mxn+10],sz[mxn+10];
    void init(){for(int i=0;i<=mxn;i++)pa[i]=i,sz[i]=1;}
    int find(int u){
        if(pa[u]==u)return u;
        return pa[u]=find(pa[u]);
    }
    void merg(int u,int v){
        int a=find(u),b=find(v);
        if(a==b)return;
        if(sz[a]>=sz[b]){
            pa[b]=a;
            sz[a]+=sz[b];
            return;
        }
        pa[a]=b;
        sz[b]+=sz[a];
    }
};
/*
void build(vector<vector<int>>v){
    for(auto i:v){
        for(auto j:i)cout<<j<<" ";
        cout<<'\n';
    }
}
*/
vector<int>v[mxn+10];
int construct(vector<vector<int>>p){
    int n=p.size();
    dsu one,two;
    vector<vector<int>>ans(n,vector<int>(n));
    one.init();
    two.init();
    for(int i=0;i<n;i++){
        for(int j=0;j<i;j++){
            if(p[i][j]==1)one.merg(i,j);
            if(p[i][j]!=p[j][i])return 0;
        }
    }
    for(int i=0;i<n;i++)v[one.pa[i]].pb(i),one.pa[i]=one.find(i);
    set<int>s;
    //case one
    for(int i=0;i<n;i++)if(one.pa[i]!=i)ans[i][one.pa[i]]=1,ans[one.pa[i]][i]=1;
    for(int i=0;i<n;i++){
        for(int j=0;j<i;j++){
            if(p[i][j]==2)two.merg(one.pa[i],one.pa[j]);//grouping
        }
    }
    for(int i=0;i<n;i++)two.pa[i]=two.find(i);
    memset(last,-1,sizeof last);
    memset(first,-1,sizeof first);
    //cyclic connection in group
    for(int i=0;i<n;i++){
        if(v[i].size())continue;
        vector<int>check(n,-1);
        for(int j=0;j<n;j++)check[j]=p[i][j];
        for(int j=1;j<v[i].size();j++){
            for(int k=0;k<n;k++)if(check[j]!=p[v[i][j]][k])return 0;
        }
    }
    for(int i=0;i<n;i++){
        if(one.pa[i]!=i)continue;
        if(last[two.pa[i]]==-1)last[two.pa[i]]=i,first[two.pa[i]]=i;
        else{
            ans[last[two.pa[i]]][i]=1;
            ans[i][last[two.pa[i]]]=1;
            last[two.pa[i]]=i;
        }
    }
    //finish the last connect
    for(int i=0;i<n;i++){
        if(last[i]==-1)continue;
        if(first[i]==last[i])continue;
        if(ans[first[i]][last[i]]){//already connected
            if(v[first[i]].size()||v[last[i]].size()){
                //connect to second node
                if(v[last[i]].size())swap(first[i],last[i]);
                ans[first[i]][v[last[i]][1]]=1;
                ans[v[last[i]][1]][first[i]]=1;
            }
            else return 0;
            continue;
        }
        ans[first[i]][last[i]]=1;
        ans[last[i]][first[i]]=1;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(p[i][j]!=0)continue;
            if((two.pa[i]==two.pa[j])||(one.pa[i]==one.pa[j]))return 0;
        }
    }
    build(ans);
}
/*
int32_t main(){
    fastio
    int n;cin>>n;
    vector<vector<int>>in(n,vector<int>(n));
    for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>in[i][j];
    construct(in);
    return 0;
}*/

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:92:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |         for(int j=1;j<v[i].size();j++){
      |                     ~^~~~~~~~~~~~
supertrees.cpp: In function 'void setIO(std::string)':
supertrees.cpp:31:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:32:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:66:44: warning: control reaches end of non-void function [-Wreturn-type]
   66 |     vector<vector<int>>ans(n,vector<int>(n));
      |                                            ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 600 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 600 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 600 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 600 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -