結果

問題 No.1865 Make Cycle
ユーザー 👑 kmjpkmjp
提出日時 2022-03-04 23:00:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 538 ms / 3,000 ms
コード長 2,083 bytes
コンパイル時間 2,508 ms
コンパイル使用メモリ 210,344 KB
実行使用メモリ 19,772 KB
最終ジャッジ日時 2023-09-26 02:14:04
合計ジャッジ時間 8,867 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 253 ms
16,576 KB
testcase_01 AC 161 ms
15,256 KB
testcase_02 AC 291 ms
17,300 KB
testcase_03 AC 136 ms
16,688 KB
testcase_04 AC 283 ms
18,256 KB
testcase_05 AC 48 ms
17,588 KB
testcase_06 AC 253 ms
16,772 KB
testcase_07 AC 231 ms
16,544 KB
testcase_08 AC 383 ms
18,476 KB
testcase_09 AC 47 ms
17,780 KB
testcase_10 AC 317 ms
18,332 KB
testcase_11 AC 286 ms
17,744 KB
testcase_12 AC 249 ms
16,620 KB
testcase_13 AC 233 ms
16,532 KB
testcase_14 AC 198 ms
15,628 KB
testcase_15 AC 309 ms
17,580 KB
testcase_16 AC 370 ms
18,472 KB
testcase_17 AC 42 ms
15,960 KB
testcase_18 AC 47 ms
18,012 KB
testcase_19 AC 538 ms
19,772 KB
testcase_20 AC 4 ms
9,320 KB
testcase_21 AC 4 ms
9,332 KB
testcase_22 AC 4 ms
9,268 KB
testcase_23 AC 5 ms
9,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,Q;
int A[101010],B[101010];

class SCC {
public:
	static const int MV = 125000;
	vector<vector<int> > SC; int NV,GR[MV];
private:
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
public:
	void init(int NV) { this->NV=NV; for(int i=0;i<NV;i++) { E[i].clear(); RE[i].clear();}}
	void add_edge(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) { int i; vis[cu]=1; GR[cu]=ind; SC[ind].push_back(cu);
		FOR(i,RE[cu].size()) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0,i; SC.clear(); SC.resize(NV); NUM.clear();
		assert(NV);
		FOR(i,NV) vis[i]=0; FOR(i,NV) if(!vis[i]) dfs(i); FOR(i,NV) vis[i]=0;
		for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]]){
			SC[c].clear(); revdfs(NUM[i],c); sort(SC[c].begin(),SC[c].end()); c++;
		}
		SC.resize(c);
	}
};

SCC scc;
int hoge(int Q) {
	scc.init(N);
	int i;
	FOR(i,Q) scc.add_edge(A[i],B[i]);
	scc.scc();
	return scc.SC.size()!=N;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>Q;
	FOR(i,Q) {
		cin>>A[i]>>B[i];
		A[i]--;
		B[i]--;
	}
	if(hoge(Q)==0) {
		cout<<-1<<endl;
		return;
	}
	for(i=20;i>=0;i--) if(hoge(Q-(1<<i))) Q-=1<<i;
	cout<<Q<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0