#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
/*const int MOD = ;
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1) ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}
inline void add(int &x, int y) {
x += y;
mod(x);
}
inline void sub(int &x, int y) {
x += MOD - y;
mod(x);
}
inline void mul(int &x, int y) {
x = (1LL * x * y) % MOD;
}
inline int inv(int x) {
return lgput(x, MOD - 2);
}*/
/*int fact[], invfact[];
inline void prec(int n) {
fact[0] = 1;
for(int i = 1; i <= n; i++) {
fact[i] = (1LL * fact[i - 1] * i) % MOD;
}
invfact[n] = lgput(fact[n], MOD - 2);
for(int i = n - 1; i >= 0; i--) {
invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
}
}
inline int comb(int n, int k) {
if(n < k) return 0;
return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}*/
using namespace std;
const int MAXN = (int) 3e5;
vector <int> g[MAXN + 1], aux[MAXN + 1];
bool vis[MAXN + 1];
void go(int nod) {
vis[nod] = 1;
for(auto it : g[nod]) {
if(vis[it] == 0) {
go(it);
}
}
}
vector <int> top;
void topSort(int nod) {
vis[nod] = 1;
for(auto it : g[nod]) {
if(vis[it] == 0) {
topSort(it);
}
}
top.push_back(nod);
}
vector <int> dag[MAXN + 1];
int idc[MAXN + 1];
void dfs(int nod, int sz) {
idc[nod] = sz;
vis[nod] = 1;
for(auto it : aux[nod]) {
if(vis[it] == 0) {
dfs(it, sz);
}
}
}
int l[MAXN + 1], r[MAXN + 1];
void solve(int nod) {
vis[nod] = 1;
for(auto it : dag[nod]) {
if(vis[it] == 0) {
solve(it);
}
l[nod] = min(l[it], l[nod]);
r[nod] = max(r[it], r[nod]);
}
}
int main() {
#if 0
ifstream cin("A.in");
ofstream cout("A.out");
#endif
int i, n, m, a, b;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> a >> b;
vector <int> s, d, x(n + 1), y(n + 1);
for(i = 1; i <= n; i++) {
cin >> x[i] >> y[i];
if(x[i] == 0) s.push_back(i);
if(x[i] == a) d.push_back(i);
}
for(i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
g[x].push_back(y);
aux[y].push_back(x);
if(z == 2) {
g[y].push_back(x);
aux[x].push_back(y);
}
}
for(auto it : s) {
if(vis[it] == 0)
go(it);
}
sort(d.begin(), d.end(), [&](const int &a, const int &b) {
return y[a] < y[b];
});
vector <int> sp(d.size());
for(i = 0; i < d.size(); i++) {
sp[i] = (i > 0 ? sp[i - 1] : 0) + 1 - vis[d[i]];
}
fill(vis + 1, vis + n + 1, 0);
for(auto it : s) {
if(vis[it] == 0)
topSort(it);
}
fill(vis + 1, vis + n + 1, 0);
int sz = 0;
for(i = top.size() - 1; i >= 0; i--) {
if(vis[top[i]] == 0)
dfs(top[i], ++sz);
}
for(i = 1; i <= n; i++) {
for(auto it : g[i]) {
if(idc[i] != idc[it]) {
dag[idc[i]].push_back(idc[it]);
}
}
}
fill(vis + 1, vis + n + 1, 0);
fill(l + 1, l + n + 1, n + 1), fill(r + 1, r + n + 1, -1);
for(i = 0; i < d.size(); i++) {
l[idc[d[i]]] = min(l[idc[d[i]]], i);
r[idc[d[i]]] = max(r[idc[d[i]]], i);
}
for(auto it : s) {
if(vis[idc[it]] == 0)
solve(idc[it]);
}
auto get = [&](int l, int r) {
return sp[r] - (l > 0 ? sp[l - 1] : 0);
};
sort(s.begin(), s.end(), [&](const int &a, const int &b) {
return y[a] > y[b];
});
for(auto nod : s) {
int it = idc[nod];
if(r[it] == -1) { cout << 0 << "\n"; continue; }
cout << r[it] - l[it] + 1 - get(l[it], r[it]) << "\n";
}
return 0;
}
Compilation message
tra.cpp: In function 'int main()':
tra.cpp:150:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i = 0; i < d.size(); i++) {
~~^~~~~~~~~~
tra.cpp:176:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i = 0; i < d.size(); i++) {
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21496 KB |
Output is correct |
3 |
Correct |
21 ms |
21516 KB |
Output is correct |
4 |
Correct |
21 ms |
21496 KB |
Output is correct |
5 |
Correct |
21 ms |
21496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
21532 KB |
Output is correct |
2 |
Correct |
21 ms |
21624 KB |
Output is correct |
3 |
Correct |
20 ms |
21496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
21496 KB |
Output is correct |
2 |
Correct |
22 ms |
21496 KB |
Output is correct |
3 |
Correct |
22 ms |
21624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
21624 KB |
Output is correct |
2 |
Correct |
27 ms |
22136 KB |
Output is correct |
3 |
Correct |
26 ms |
22008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
23160 KB |
Output is correct |
2 |
Correct |
89 ms |
26712 KB |
Output is correct |
3 |
Correct |
59 ms |
25080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
25204 KB |
Output is correct |
2 |
Correct |
118 ms |
29888 KB |
Output is correct |
3 |
Correct |
87 ms |
28532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
28916 KB |
Output is correct |
2 |
Correct |
214 ms |
36620 KB |
Output is correct |
3 |
Correct |
311 ms |
36368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
30024 KB |
Output is correct |
2 |
Correct |
209 ms |
35288 KB |
Output is correct |
3 |
Correct |
335 ms |
36852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
347 ms |
35540 KB |
Output is correct |
2 |
Correct |
367 ms |
46920 KB |
Output is correct |
3 |
Correct |
659 ms |
50596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
517 ms |
44336 KB |
Output is correct |
2 |
Correct |
699 ms |
61924 KB |
Output is correct |
3 |
Correct |
655 ms |
53608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1115 ms |
52572 KB |
Output is correct |
2 |
Correct |
707 ms |
63460 KB |
Output is correct |
3 |
Correct |
1171 ms |
69548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
36952 KB |
Output is correct |
2 |
Correct |
744 ms |
68260 KB |
Output is correct |
3 |
Correct |
990 ms |
66140 KB |
Output is correct |
4 |
Correct |
1236 ms |
75876 KB |
Output is correct |