結果

問題 No.3103 Butterfly Effect
ユーザー shobonvip
提出日時 2025-04-05 01:04:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,175 bytes
コンパイル時間 2,281 ms
コンパイル使用メモリ 205,340 KB
実行使用メモリ 61,184 KB
最終ジャッジ日時 2025-04-05 01:05:02
合計ジャッジ時間 34,429 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 46 TLE * 1 -- * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

// 多分 AC だと思う
#include<bits/stdc++.h>
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n, q; cin >> n >> q;
	vector<int> dp(n, q+1);
	dp[0] = -1;
	vector<set<pair<int,int>>> horyu(n);
	int ans = 1;

	for (int num=0; num<q; num++) {
		int p; cin >> p;
		p--;
		int x, y; cin >> x >> y;
		x--; y--;
		if (dp[x] < p && dp[y] < p) {

		}else if (dp[x] > p && dp[y] > p) {
			horyu[x].insert(pair(p,y));
			horyu[y].insert(pair(p,x));
		}else{
			if (dp[y] < p) {
				swap(x, y);
			}
			assert(dp[x] < p && dp[y] > p);

			if (dp[y] == q+1) ans++;
			dp[y] = p;

			vector<pair<int,int>> mada;
			mada.push_back(pair(p,y));
			while (!mada.empty()) {
				auto [t,i] = mada.back();
				pair<int,int> now = pair(t,-1);
				mada.pop_back();
				while (true) {
					auto itr = horyu[i].upper_bound(now);
					if (itr == horyu[i].end()) {
						break;
					}

					int tim = (*itr).first;
					int z = (*itr).second;
					if (dp[z] > tim) {
						if (dp[z] == q+1) ans++;
						dp[z] = tim;
						horyu[i].erase(itr);
						mada.push_back(pair(tim,z));
					}
					now = pair(tim, z);
				}
			}
		}
		cout << ans << '\n';
	}

}
0