結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 164 ms
6,836 KB
testcase_01 AC 103 ms
6,196 KB
testcase_02 AC 196 ms
7,164 KB
testcase_03 AC 67 ms
6,528 KB
testcase_04 AC 120 ms
7,424 KB
testcase_05 AC 160 ms
7,532 KB
testcase_06 AC 160 ms
7,008 KB
testcase_07 AC 150 ms
6,820 KB
testcase_08 AC 207 ms
7,820 KB
testcase_09 AC 153 ms
7,476 KB
testcase_10 AC 178 ms
7,628 KB
testcase_11 AC 165 ms
7,464 KB
testcase_12 AC 142 ms
6,880 KB
testcase_13 AC 161 ms
6,872 KB
testcase_14 AC 110 ms
6,424 KB
testcase_15 AC 187 ms
7,484 KB
testcase_16 AC 205 ms
7,896 KB
testcase_17 AC 144 ms
6,792 KB
testcase_18 AC 154 ms
7,736 KB
testcase_19 AC 253 ms
8,648 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 2 ms
5,248 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 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