Submission #584610

#TimeUsernameProblemLanguageResultExecution timeMemory
584610TimDee슈퍼트리 잇기 (IOI20_supertrees)C++14
21 / 100
1068 ms22980 KiB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for (int i=0; i<n; ++i)
#define all(a) a.begin(), a.end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define pi pair<int,int>
#define f first
#define s second

int mirror(vector<vector<int>>&a) {
  int n=a.size();
  for (int i=0; i<n; ++i) {
    for (int j=i+1; j<n; ++j) {
      if (a[i][j]!=a[j][i]) return 0;
      if (a[i][j]==3) return 0;
    }
  }
  forn(i,n) if (a[i][i]!=1) return 0;
  return 1;
}

int check(vector<vector<int>>&p,vector<int>&one,vector<int>&vis) {
  if (one.size()<=0) return 1;

  queue<int> q;
  forn(i,one.size()) {
    q.push(one[i]);
  }
  //forn(i,p.size()) cout<<vis[i]<<' '; cout<<'\n';
  while (!q.empty()) {
    int v=q.front(); q.pop();
    //cout<<"q "<<v<<'\n';
    forn(i,p.size()) {
      if (i==v) continue;
      if (p[v][i]==1 && vis[i]!=10) return 0;
    }
  }
  forn(i,one.size()) vis[one[i]]=1;
  return 1;
}

void colordfs(vector<int>&dp, vector<int>&vis, vector<vector<int>>&a, int v, int esk) {
  if (v==esk) return;
  if (vis[v]==2) return;
  vis[v]=2;
  dp[v]=2;
  forn(i,dp.size()) if (a[v][i]) colordfs(dp,vis,a,i,esk);
}

vector<int> vis(1e3,0);

int dfs(int u, int start, vector<vector<int>>&dp, vector<vector<int>>&a) {
    vis[u] = true;
    //cout<<u<<": ";
    //cout<<dp[start][u]<<'\n';
    if(++dp[start][u] >= 3) {
        //cout<<start<<' '<<u<<'\n';
        return 0;
    }

    forn(i,a.size()) {
        if(a[u][i] && !vis[i]) {
          //cout<<"call dfs "<<u<<' '<<i<<'\n';
          int x=dfs(i, start, dp, a);
          if (!x) return 0;
        }
    }
    vis[u] = false;
    return 1;
}

int pathverif(vector<vector<int>>&a, vector<vector<int>>&p) {
  int n=a.size();
  vector<vector<int>> dp(n);
  forn(i,n) dp[i].assign(n,0);
  forn(i,n) {
    int x=dfs(i,i,dp,a);
    if (!x) return 0;
  }
  forn(i,n) {
    forn(j,n) //cout<<dp[i][j]<<' ';
    if (dp[i][j]!=p[i][j]) return 0;
    //cout<<'\n';
  }
  return 1;
}

int bfs(vector<vector<int>>&p, vector<int>&vis, int v, vector<pair<int,int>>&edges) {

  vector<int> one, two;
  vector<int> cycle;

  int n=p.size();

  forn(i,n) {
    if (i==v) continue;
    if (p[v][i]==1) one.pb(i);
    else if (p[v][i]==2) two.pb(i);
  }

  forn(i,one.size()) vis[one[i]]=10;
  forn(i,two.size()) vis[two[i]]=10;
  vis[v]=10;

  forn(i,one.size()) {
    forn(j,one.size()) {
      if (i==j) continue;
      if (p[one[i]][one[j]]!=1) return 0;
    }
  }
  forn(i,one.size()) edges.pb(mp(v,one[i]));

  //forn(i,one.size()) vis[one[i]]=1;
  //forn(i,two.size()) vis[two[i]]=1;
  //vis[v]=1;
 
  if (!two.size()) {
    //forn(i,one.size()) vis[one[i]]=1;
    //vis[v]=1;
    return check(p,one,vis);
  } else if (two.size()==1) return 0;
  else {
    forn(i,one.size()) {
      forn(j,two.size()) {
        if (p[one[i]][two[j]]!=2) return 0;
      }
    }
    cycle={v};
    forn(i,two.size()) {
      int foo=1;
      forn(j,cycle.size()) {
        if (p[two[i]][cycle[j]]==2) continue;
        else {foo=0; break;}
      }
      if (foo) cycle.pb(two[i]);
    }
  }

  queue<int> q;
  forn(i,cycle.size()) {
    if (cycle[i]!=v) q.push(cycle[i]);
  }

  while (!q.empty()) {
 
    int v=q.front(); q.pop();
    
    vector<int> one, two;

    forn(i,n) {
      if (i==v) continue;
      if (p[v][i]==1) one.pb(i);
      else if (p[v][i]==2) two.pb(i);
    }

    forn(i,one.size()) {
      forn(j,one.size()) {
        if (i==j) continue;
        if (p[one[i]][one[j]]!=1) return 0;
      }
    }
    forn(i,one.size()) edges.pb(mp(v,one[i]));

    forn(i,one.size()) if (vis[one[i]]!=10) return 0;
    forn(i,two.size()) if (vis[two[i]]!=10) return 0;
    if (vis[v]!=10) return 0;
 
    if (!two.size()) {
      continue;
    } else if (two.size()==1) return 0;
    else {
      forn(i,one.size()) {
        forn(j,two.size()) {
          if (p[one[i]][two[j]]!=2) return 0;
        }
      }
    }
  
  }

  if (cycle.size()<=2) return 0;

  forn(i,cycle.size()) {
    edges.pb(mp(cycle[i],cycle[(i+1)%cycle.size()]));
  }

  forn(i,one.size()) vis[one[i]]=1;
  forn(i,two.size()) vis[two[i]]=1;
  vis[v]=1;

  return 1;

}

int construct(std::vector<std::vector<int>> p) {
  int n = p.size();
  
  vector<vector<int>> a(n); 
  forn(i,n) a[i].assign(n,0);
 
  if (!mirror(p)) return 0;
 
  queue<int> q;
  q.push(0);
 
  vector<pair<int,int>> edges;
  vector<int> vis(n,0);
 
  forn(i,n) {
    if (vis[i]) continue;
    int x=bfs(p,vis,i,edges);
    if (x==0) return 0;
  }
 
  for (auto E:edges) {
    int u=E.f, v=E.s;
    a[u][v]=1;
    a[v][u]=1;
  }

  if (!pathverif(a,p)) return 0;
 
  build(a);
  return 1;
}

Compilation message (stderr)

supertrees.cpp: In function 'int check(std::vector<std::vector<int> >&, std::vector<int>&, std::vector<int>&)':
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
   28 |   forn(i,one.size()) {
      |        ~~~~~~~~~~~~               
supertrees.cpp:28:3: note: in expansion of macro 'forn'
   28 |   forn(i,one.size()) {
      |   ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
   35 |     forn(i,p.size()) {
      |          ~~~~~~~~~~               
supertrees.cpp:35:5: note: in expansion of macro 'forn'
   35 |     forn(i,p.size()) {
      |     ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
   40 |   forn(i,one.size()) vis[one[i]]=1;
      |        ~~~~~~~~~~~~               
supertrees.cpp:40:3: note: in expansion of macro 'forn'
   40 |   forn(i,one.size()) vis[one[i]]=1;
      |   ^~~~
supertrees.cpp: In function 'void colordfs(std::vector<int>&, std::vector<int>&, std::vector<std::vector<int> >&, int, int)':
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
   49 |   forn(i,dp.size()) if (a[v][i]) colordfs(dp,vis,a,i,esk);
      |        ~~~~~~~~~~~                
supertrees.cpp:49:3: note: in expansion of macro 'forn'
   49 |   forn(i,dp.size()) if (a[v][i]) colordfs(dp,vis,a,i,esk);
      |   ^~~~
supertrees.cpp: In function 'int dfs(int, int, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
   63 |     forn(i,a.size()) {
      |          ~~~~~~~~~~               
supertrees.cpp:63:5: note: in expansion of macro 'forn'
   63 |     forn(i,a.size()) {
      |     ^~~~
supertrees.cpp: In function 'int bfs(std::vector<std::vector<int> >&, std::vector<int>&, int, std::vector<std::pair<int, int> >&)':
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  103 |   forn(i,one.size()) vis[one[i]]=10;
      |        ~~~~~~~~~~~~               
supertrees.cpp:103:3: note: in expansion of macro 'forn'
  103 |   forn(i,one.size()) vis[one[i]]=10;
      |   ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  104 |   forn(i,two.size()) vis[two[i]]=10;
      |        ~~~~~~~~~~~~               
supertrees.cpp:104:3: note: in expansion of macro 'forn'
  104 |   forn(i,two.size()) vis[two[i]]=10;
      |   ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  107 |   forn(i,one.size()) {
      |        ~~~~~~~~~~~~               
supertrees.cpp:107:3: note: in expansion of macro 'forn'
  107 |   forn(i,one.size()) {
      |   ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  108 |     forn(j,one.size()) {
      |          ~~~~~~~~~~~~             
supertrees.cpp:108:5: note: in expansion of macro 'forn'
  108 |     forn(j,one.size()) {
      |     ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  113 |   forn(i,one.size()) edges.pb(mp(v,one[i]));
      |        ~~~~~~~~~~~~               
supertrees.cpp:113:3: note: in expansion of macro 'forn'
  113 |   forn(i,one.size()) edges.pb(mp(v,one[i]));
      |   ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  125 |     forn(i,one.size()) {
      |          ~~~~~~~~~~~~             
supertrees.cpp:125:5: note: in expansion of macro 'forn'
  125 |     forn(i,one.size()) {
      |     ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  126 |       forn(j,two.size()) {
      |            ~~~~~~~~~~~~           
supertrees.cpp:126:7: note: in expansion of macro 'forn'
  126 |       forn(j,two.size()) {
      |       ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  131 |     forn(i,two.size()) {
      |          ~~~~~~~~~~~~             
supertrees.cpp:131:5: note: in expansion of macro 'forn'
  131 |     forn(i,two.size()) {
      |     ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  133 |       forn(j,cycle.size()) {
      |            ~~~~~~~~~~~~~~         
supertrees.cpp:133:7: note: in expansion of macro 'forn'
  133 |       forn(j,cycle.size()) {
      |       ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  142 |   forn(i,cycle.size()) {
      |        ~~~~~~~~~~~~~~             
supertrees.cpp:142:3: note: in expansion of macro 'forn'
  142 |   forn(i,cycle.size()) {
      |   ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  158 |     forn(i,one.size()) {
      |          ~~~~~~~~~~~~             
supertrees.cpp:158:5: note: in expansion of macro 'forn'
  158 |     forn(i,one.size()) {
      |     ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  159 |       forn(j,one.size()) {
      |            ~~~~~~~~~~~~           
supertrees.cpp:159:7: note: in expansion of macro 'forn'
  159 |       forn(j,one.size()) {
      |       ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  164 |     forn(i,one.size()) edges.pb(mp(v,one[i]));
      |          ~~~~~~~~~~~~             
supertrees.cpp:164:5: note: in expansion of macro 'forn'
  164 |     forn(i,one.size()) edges.pb(mp(v,one[i]));
      |     ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  166 |     forn(i,one.size()) if (vis[one[i]]!=10) return 0;
      |          ~~~~~~~~~~~~             
supertrees.cpp:166:5: note: in expansion of macro 'forn'
  166 |     forn(i,one.size()) if (vis[one[i]]!=10) return 0;
      |     ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  167 |     forn(i,two.size()) if (vis[two[i]]!=10) return 0;
      |          ~~~~~~~~~~~~             
supertrees.cpp:167:5: note: in expansion of macro 'forn'
  167 |     forn(i,two.size()) if (vis[two[i]]!=10) return 0;
      |     ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  174 |       forn(i,one.size()) {
      |            ~~~~~~~~~~~~           
supertrees.cpp:174:7: note: in expansion of macro 'forn'
  174 |       forn(i,one.size()) {
      |       ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  175 |         forn(j,two.size()) {
      |              ~~~~~~~~~~~~         
supertrees.cpp:175:9: note: in expansion of macro 'forn'
  175 |         forn(j,two.size()) {
      |         ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  185 |   forn(i,cycle.size()) {
      |        ~~~~~~~~~~~~~~             
supertrees.cpp:185:3: note: in expansion of macro 'forn'
  185 |   forn(i,cycle.size()) {
      |   ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  189 |   forn(i,one.size()) vis[one[i]]=1;
      |        ~~~~~~~~~~~~               
supertrees.cpp:189:3: note: in expansion of macro 'forn'
  189 |   forn(i,one.size()) vis[one[i]]=1;
      |   ^~~~
supertrees.cpp:4:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for (int i=0; i<n; ++i)
......
  190 |   forn(i,two.size()) vis[two[i]]=1;
      |        ~~~~~~~~~~~~               
supertrees.cpp:190:3: note: in expansion of macro 'forn'
  190 |   forn(i,two.size()) vis[two[i]]=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...