답안 #1116041

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1116041 2024-11-21T08:11:40 Z noyancanturk Potemkin cycle (CEOI15_indcyc) C++17
60 / 100
50 ms 9300 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back

int main(){
  int n,m;
  cin>>n>>m;
  vector<int>v[n+100];
  unordered_set<int>u[n+100];
  for(int i=0;i<m;i++){
    int x,y;
    cin>>x>>y;
    v[x].pb(y);
    v[y].pb(x);
    u[x].insert(y);
    u[y].insert(x);
  }
  int layer[n+100]{};
  vector<int>par[n+100];
  queue<int>q;
  q.push(1);
  layer[1]=1;
  while(q.size()){
    int node=q.front();
    q.pop();
    for(int j:v[node]){
      if(layer[node]<layer[j])par[j].pb(node);
      if(!layer[j]){
        par[j].pb(node);
        layer[j]=layer[node]+1;
        q.push(j);
      }
    }
  }
  int x=0,y=0,z=0;
  for(int i=1;i<=n;i++){
    for(int j:par[i]){
      for(int k:par[i]){
        if(j==k)continue;
        if(!u[j].count(k)){
          x=i,y=j,z=k;
          break;
        }
      }
      if(x)break;
    }
    if(x)break;
  }
  if(x){
    vector<int>chain1,chain2;
    chain1.pb(x);
    chain1.pb(y);
    chain2.pb(z);
    while(!u[chain1.back()].count(chain2.back())){
      assert(layer[chain1.back()]==layer[chain2.back()]);
      int ng1=par[chain1.back()][0];
      int ng2=par[chain2.back()][0];
      if(u[chain1.back()].count(ng2)){
        chain2.pb(ng2);
        break;
      }
      if(u[chain2.back()].count(ng1)){
        chain1.pb(ng1);
        break;
      }
      chain1.pb(ng1);
      chain2.pb(ng2);
    }
    for(int i=0;i<chain1.size();i++){
      cout<<chain1[i]<<' ';
    }
    for(int i=chain2.size()-1;0<=i;i--){
      cout<<chain2[i]<<' ';
    }
    return 0;
  }
  int w=0;
  for(int i=1;i<=n;i++){
    for(int j:v[i]){
      if(layer[i]!=layer[j])continue;
      int g1=0,g2=0;
      for(int k:par[i]){
        if(!u[j].count(k)){
          g1=k;
          break;
        }
      }
      for(int k:par[j]){
        if(!u[i].count(k)){
          g2=k;
          break;
        }
      }
      if(g1&&g2){
        x=i,y=j,w=g1,z=g2;
        break;
      }
    }
    if(x)break;
  }
  if(x){
    vector<int>chain1,chain2;
    chain1.pb(x);
    chain1.pb(w);
    chain2.pb(y);
    chain2.pb(z);
    while(!u[chain1.back()].count(chain2.back())){
      assert(layer[chain1.back()]==layer[chain2.back()]);
      int ng1=par[chain1.back()][0];
      int ng2=par[chain2.back()][0];
      if(u[chain1.back()].count(ng2)){
        chain2.pb(ng2);
        break;
      }
      if(u[chain2.back()].count(ng1)){
        chain1.pb(ng1);
        break;
      }
      chain1.pb(ng1);
      chain2.pb(ng2);
    }
    for(int i=0;i<chain1.size();i++){
      cout<<chain1[i]<<' ';
    }
    for(int i=chain2.size()-1;0<=i;i--){
      cout<<chain2[i]<<' ';
    }
    return 0;
  }
  cout<<"no";
  return 0;
}

Compilation message

indcyc.cpp: In function 'int main()':
indcyc.cpp:69:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |     for(int i=0;i<chain1.size();i++){
      |                 ~^~~~~~~~~~~~~~
indcyc.cpp:122:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |     for(int i=0;i<chain1.size();i++){
      |                 ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 504 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 592 KB Output is correct
2 Correct 3 ms 592 KB Output is correct
3 Correct 5 ms 1104 KB Output is correct
4 Correct 4 ms 1104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 592 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 5148 KB Output is correct
2 Correct 13 ms 2384 KB Output is correct
3 Correct 50 ms 5200 KB Output is correct
4 Correct 16 ms 2512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 2384 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 46 ms 9300 KB Expected integer, but "no" found
2 Halted 0 ms 0 KB -