#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define pb push_back
#define INF 100000000000000000
#define fi first
#define se second
//#define cin fin
//#define cout fout
using namespace std;
double const EPS = 1e-14;
typedef long long ll;
const ll P = 10007;
const ll mod = 1e9 + 7;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // find_by_order, order_of_key
//ofstream fout("cardgame.out");
//ifstream fin("cardgame.in");
const int M = 3e5 + 5;
bool ok[M];
vector<pair<int,int>> a, b;
vector<int> v[M], v2[M];
vector<pair<int,pair<int,int>>> order;
pair<int,int> ans[M];
void check() {
queue<int> q;
for(int i = 0; i < a.size(); i++) {
ok[a[i].se] = 1; q.push(a[i].se);
}
while(!q.empty()) {
int tp = q.front();
q.pop();
for(auto i : v[tp]) {
if(ok[i] == 0){
ok[i] = 1;
q.push(i);
}
}
}
}
void dfs(int s, int val) {
ok[s] = 1;
for(auto i : v2[s]) {
if(ok[i] == 0) {
if(ans[i].fi == -1) ans[i].fi = val;
else ans[i].se = val;
dfs(i,val);
}
}
}
int main()
{
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
int n, m, A, B; cin >> n >> m >> A >> B;
for(int i = 0; i < n; i++) {
int x, y; cin >> x >> y;
if(x == 0) {
a.push_back({y,i+1});
}
else if(x == A) {
b.push_back({y,i+1});
}
}
for(int i = 1; i <= n; i++) {
ans[i].fi = -1; ans[i].se = -1;
}
for(int i = 0; i < m; i++) {
int x, y, tp;
cin >> x >> y >> tp;
v2[y].push_back(x);
v[x].push_back(y);
if(tp == 2) {
v[y].push_back(x);
v2[x].push_back(y);
}
}
check();
sort(b.begin(),b.end());
int cnt = 0;
for(int i = 0; i < b.size(); i++) {
if(ok[b[i].se] == 1) {
order.push_back({b[i].fi,{b[i].se,cnt++}});
}
}
sort(a.begin(),a.end());
sort(order.begin(),order.end());
for(int i = 1; i <= n; i++) ok[i] = 0;
for(int i = 0; i < order.size(); i++) {
dfs(order[i].se.fi,order[i].se.se);
}
for(int i = 1; i <= n; i++) ok[i] = 0;
for(int i = order.size()-1; i >= 0; i--) {
dfs(order[i].se.fi,order[i].se.se);
}
for(int i = a.size()-1; i >= 0; i--) {
if(ans[a[i].se].fi == -1) cout << 0 << endl;
else cout << abs(ans[a[i].se].fi-ans[a[i].se].se)+1 << endl;
}
return 0;
}
Compilation message
tra.cpp: In function 'void check()':
tra.cpp:30:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
tra.cpp: In function 'int main()':
tra.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 0; i < b.size(); i++) {
| ~~^~~~~~~~~~
tra.cpp:92:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int i = 0; i < order.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14416 KB |
Output is correct |
2 |
Correct |
10 ms |
14416 KB |
Output is correct |
3 |
Correct |
10 ms |
14416 KB |
Output is correct |
4 |
Correct |
10 ms |
14484 KB |
Output is correct |
5 |
Correct |
11 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14416 KB |
Output is correct |
2 |
Correct |
11 ms |
14416 KB |
Output is correct |
3 |
Correct |
11 ms |
14492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14416 KB |
Output is correct |
2 |
Correct |
10 ms |
14416 KB |
Output is correct |
3 |
Correct |
11 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14672 KB |
Output is correct |
2 |
Correct |
14 ms |
14928 KB |
Output is correct |
3 |
Correct |
12 ms |
14672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
15952 KB |
Output is correct |
2 |
Correct |
42 ms |
19892 KB |
Output is correct |
3 |
Correct |
26 ms |
16992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
17480 KB |
Output is correct |
2 |
Correct |
55 ms |
21520 KB |
Output is correct |
3 |
Correct |
34 ms |
19272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
19272 KB |
Output is correct |
2 |
Correct |
81 ms |
26836 KB |
Output is correct |
3 |
Correct |
103 ms |
26408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
21852 KB |
Output is correct |
2 |
Correct |
75 ms |
26312 KB |
Output is correct |
3 |
Correct |
106 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
24136 KB |
Output is correct |
2 |
Correct |
161 ms |
34104 KB |
Output is correct |
3 |
Correct |
261 ms |
37712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
31592 KB |
Output is correct |
2 |
Correct |
299 ms |
47612 KB |
Output is correct |
3 |
Correct |
262 ms |
39140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
679 ms |
43844 KB |
Output is correct |
2 |
Correct |
302 ms |
49264 KB |
Output is correct |
3 |
Correct |
540 ms |
54088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
25432 KB |
Output is correct |
2 |
Correct |
388 ms |
51876 KB |
Output is correct |
3 |
Correct |
393 ms |
48968 KB |
Output is correct |
4 |
Correct |
660 ms |
65096 KB |
Output is correct |