結果

問題 No.1865 Make Cycle
ユーザー shobonvipshobonvip
提出日時 2022-07-03 17:54:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 204 ms / 3,000 ms
コード長 1,165 bytes
コンパイル時間 2,099 ms
コンパイル使用メモリ 209,476 KB
実行使用メモリ 8,644 KB
最終ジャッジ日時 2024-05-07 01:20:14
合計ジャッジ時間 6,895 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 147 ms
6,896 KB
testcase_01 AC 92 ms
6,196 KB
testcase_02 AC 166 ms
7,168 KB
testcase_03 AC 59 ms
6,528 KB
testcase_04 AC 111 ms
7,296 KB
testcase_05 AC 153 ms
7,528 KB
testcase_06 AC 143 ms
7,008 KB
testcase_07 AC 137 ms
6,944 KB
testcase_08 AC 180 ms
7,816 KB
testcase_09 AC 130 ms
7,596 KB
testcase_10 AC 156 ms
7,628 KB
testcase_11 AC 146 ms
7,468 KB
testcase_12 AC 134 ms
6,880 KB
testcase_13 AC 146 ms
6,740 KB
testcase_14 AC 97 ms
6,428 KB
testcase_15 AC 179 ms
7,396 KB
testcase_16 AC 174 ms
7,764 KB
testcase_17 AC 131 ms
6,664 KB
testcase_18 AC 142 ms
7,732 KB
testcase_19 AC 204 ms
8,644 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
//using namespace atcoder;
typedef long long ll;
const ll INF = 1e18;
//typedef modint998244353 mint;
//cout << fixed << setprecision(12);

int main(){
	int n, q;
	cin >> n >> q;
	vector<int> qx(q);
	vector<int> qy(q);

	for (int i=0; i<q; i++){
		cin >> qx[i] >> qy[i];
		qx[i]--; qy[i]--;
	}

	int suki = q+1, kirai = -1;
	while (suki - kirai > 1){
		int targ = (suki + kirai) / 2;
		vector<vector<int>> ikeru(n, vector<int>(0));
		vector<int> deg(n);
		for (int i=0; i<targ; i++){
			ikeru[qx[i]].push_back(qy[i]);
			deg[qy[i]]++;
		}
		deque<int> mada(0);
		for (int i=0; i<n; i++){
			if (deg[i] == 0){
				mada.push_back(i);
			}
		}
		while (!mada.empty()){
			int i = mada.front();
			mada.pop_front();
			for (int j:ikeru[i]){
				deg[j]--;
				if (deg[j] == 0){
					mada.push_back(j);
				}
			}
		}
		bool mode = false;
		for (int i=0; i<n; i++){
			if (deg[i] > 0){
				mode = true;
				break;
			}
		}
		if (mode) suki = targ;
		else kirai = targ;
	}
	if (suki == q+1){
		cout << -1 << endl;
	}else{
		cout << suki << endl;
	}
}
0