Submission #244519

# Submission time Handle Problem Language Result Execution time Memory
244519 2020-07-04T08:49:20 Z errorgorn Potemkin cycle (CEOI15_indcyc) C++14
100 / 100
207 ms 7288 KB
//雪花飄飄北風嘯嘯
//天地一片蒼茫
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;
 
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
 
ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}
 
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
 
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
 
int n,m;
int am[1005][1005]; //id of each edge
vector<int> al[100005]; //this is actually storing the edges adjacentcy

bool vis[100005];
bool instack[100005];

vector<int> ans;

int root;
bool dfs(int i,int j){
	int id=am[i][j];
	if (vis[id]) return false;
	
	vis[id]=true;
	instack[id]=true;
	
	rep(x,1,n+1) if (x!=i &&  //dont go back to parent
	                 am[j][x] && am[i][x]==0){ //make sure that i,j,x is not triangle
		if (instack[am[j][x]]){ //this is confirm not cycle of length 3
			root=j;
			ans.push_back(i);
			return true;
		}
		else if (dfs(j,x)){
			ans.push_back(i);
			
			if (i==root){//we have reached the start of the cycle
				int s=0,e=sz(ans)-1;
				//rep(x,s,e+1) cout<<ans[x]<<" ";cout<<endl;
				
				for (int x=0;x<sz(ans);x++){
					for (int y=x+2;y<sz(ans);y++){
						if (am[ans[x]][ans[y]] && e-s>y-x){
							s=x,e=y;
						}
					}
				}
				
				rep(x,s,e+1) cout<<ans[x]<<" ";
				exit(0);
			}
			
			return true;
		}
	}
	
	instack[id]=false;
	return false;
}
 
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n>>m;
	
	int a,b;
	rep(x,1,m+1){
		cin>>a>>b;
		
		am[a][b]=am[b][a]=x;
	}
	
	rep(x,1,n+1) rep(y,1,n+1) if (am[x][y] && !vis[am[x][y]]){
		dfs(x,y);
	}
	
	cout<<"no"<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 6 ms 2688 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2816 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3072 KB Output is correct
2 Correct 7 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3968 KB Output is correct
2 Correct 9 ms 3968 KB Output is correct
3 Correct 8 ms 3840 KB Output is correct
4 Correct 13 ms 3840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3968 KB Output is correct
2 Correct 9 ms 3840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 7040 KB Output is correct
2 Correct 36 ms 6656 KB Output is correct
3 Correct 159 ms 6776 KB Output is correct
4 Correct 56 ms 6776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 6784 KB Output is correct
2 Correct 55 ms 6776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 4608 KB Output is correct
2 Correct 111 ms 5376 KB Output is correct
3 Correct 96 ms 7288 KB Output is correct
4 Correct 207 ms 6908 KB Output is correct