結果
| 問題 |
No.3103 Butterfly Effect
|
| コンテスト | |
| ユーザー |
shobonvip
|
| 提出日時 | 2025-01-13 03:16:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,285 bytes |
| コンパイル時間 | 2,125 ms |
| コンパイル使用メモリ | 206,384 KB |
| 実行使用メモリ | 64,332 KB |
| 最終ジャッジ日時 | 2025-02-12 01:56:56 |
| 合計ジャッジ時間 | 37,191 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 22 WA * 18 |
ソースコード
// fast
// O(N + Q log Q)
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, q; cin >> n >> q;
vector<int> x(q,-1), y(q,-1);
// dp[i] ... 人 i は時刻 dp[i] に知る. (dp[i]=q+1 ならまだ知らない)
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);
}
// 時刻 p の時点で dp[x] が知っており dp[y] が知らない
assert(dp[x] < p && dp[y] > p);
if (dp[y] == q+1) ans++;
dp[y] = p;
vector<pair<int,int>> mada = {pair(y,p)};
while (!mada.empty()) {
auto [i,t] = mada.back();
mada.pop_back();
while (true) {
auto itr = horyu[y].lower_bound(pair(t,0));
if (itr == horyu[y].end()) {
break;
}
int tim = (*itr).first;
int z = (*itr).second;
horyu[y].erase(itr);
if (dp[z] > tim) {
if (dp[z] == q+1) ans++;
dp[z] = tim;
mada.push_back(pair(z,tim));
}
}
}
}
cout << ans << '\n';
}
}
shobonvip