結果

問題 No.3103 Butterfly Effect
ユーザー cho435
提出日時 2025-04-12 00:02:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 519 ms / 5,000 ms
コード長 1,541 bytes
コンパイル時間 4,235 ms
コンパイル使用メモリ 261,608 KB
実行使用メモリ 62,632 KB
最終ジャッジ日時 2025-04-12 00:02:36
合計ジャッジ時間 23,537 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define all(x) begin(x), end(x)

template <typename T> bool chmin(T& x, T y) {
	return x > y ? (x = y, true) : false;
}
template <typename T> bool chmax(T& x, T y) {
	return x < y ? (x = y, true) : false;
}

struct IOST {
	IOST() {
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		cout << fixed << setprecision(20);
	}
} IOST;

void solve() {
	int n, q;
	cin >> n >> q;
	vector<set<pair<int, int>>> g(n);
	vector<int> tm(n, 1e9);
	vector<int> st(n, -1);
	tm[0] = 0;
	int ans = 1;
	rep(Qi, 0, q) {
		int p, x, y;
		cin >> p >> x >> y;
		x--, y--;
		if (tm[x] > p && tm[y] > p) {
			g[x].insert({p, y});
			g[y].insert({p, x});
			cout << ans << "\n";
			continue;
		}
		if (tm[x] < p && tm[y] < p) {
			cout << ans << "\n";
			continue;
		}
		if (tm[x] < p) swap(x, y);
		assert(tm[x] > p && tm[y] < p);
		if (tm[x] == 1e9) ans++;
		tm[x] = p;
		priority_queue<pair<int, int>, vector<pair<int, int>>,
					   greater<pair<int, int>>>
			pq;
		pq.push({p, x});
		while (!pq.empty()) {
			auto [nwt, nw] = pq.top();
			pq.pop();
			if (!chmax(st[nw], (int)Qi)) continue;
			while (1) {
				auto itr = g[nw].lower_bound({tm[nw], -1});
				if (itr == g[nw].end()) break;
				auto [nt, nx] = *itr;
				if (tm[nx] == 1e9) ans++;
				if (chmin(tm[nx], nt)) pq.push({nt, nx});
				g[nw].erase(itr);
			}
		}
		cout << ans << "\n";
	}
}

int main() {
	int t = 1;
	// cin >> t;
	rep(i, 0, t) solve();
}
0