#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
edi
*/
const int MOD = 1e9 + 7;
const int N = 3e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
vector<pii> adj[N];
vector<bool> vis(N);
void dfs1(int u){
vis[u] = 1;
for(auto [v,t] : adj[u]){
if(vis[v] or t == 2) conts;
dfs1(v);
}
}
vector<int> mn_reach(N,inf1), mx_reach(N,-inf1);
void dfs2(int u, int id){
vis[u] = 1;
amin(mn_reach[u],id);
for(auto [v,t] : adj[u]){
if(vis[v] or t == 1) conts;
dfs2(v,id);
}
}
void dfs3(int u, int id){
vis[u] = 1;
amax(mx_reach[u],id);
for(auto [v,t] : adj[u]){
if(vis[v] or t == 1) conts;
dfs3(v,id);
}
}
void solve(int test_case)
{
int n,m,A,B; cin >> n >> m >> A >> B;
vector<pii> a(n+5);
rep1(i,n) cin >> a[i].ff >> a[i].ss;
rep1(i,m){
int u,v,t; cin >> u >> v >> t;
if(t == 1){
adj[u].pb({v,1}), adj[v].pb({u,2});
}
else{
adj[u].pb({v,3}), adj[v].pb({u,3});
}
}
rep1(i,n){
if(!vis[i] and a[i].ff == 0){
dfs1(i);
}
}
vector<pii> right_good;
rep1(i,n){
if(vis[i]){
if(a[i].ff == A){
right_good.pb({a[i].ss,i});
}
}
}
sort(all(right_good));
int siz = sz(right_good);
right_good.insert(right_good.begin(),{0,0});
fill(all(vis),0);
rep1(i,siz){
int u = right_good[i].ss;
if(!vis[u]){
dfs2(u,i);
}
}
fill(all(vis),0);
rev(i,siz,1){
int u = right_good[i].ss;
if(!vis[u]){
dfs3(u,i);
}
}
vector<pii> ans;
rep1(i,n){
if(a[i].ff == 0){
int mn = mn_reach[i], mx = mx_reach[i];
int cnt = max(mx-mn+1,0);
ans.pb({a[i].ss,cnt});
}
}
sort(rall(ans));
for(auto [y,c] : ans){
cout << c << endl;
}
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9820 KB |
Output is correct |
2 |
Correct |
3 ms |
9784 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
3 ms |
9820 KB |
Output is correct |
5 |
Correct |
3 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
7 ms |
10332 KB |
Output is correct |
3 |
Correct |
5 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
10332 KB |
Output is correct |
2 |
Correct |
32 ms |
14552 KB |
Output is correct |
3 |
Correct |
19 ms |
12188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
11612 KB |
Output is correct |
2 |
Correct |
43 ms |
15980 KB |
Output is correct |
3 |
Correct |
25 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
13144 KB |
Output is correct |
2 |
Correct |
72 ms |
20688 KB |
Output is correct |
3 |
Correct |
120 ms |
22400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
15180 KB |
Output is correct |
2 |
Correct |
70 ms |
20304 KB |
Output is correct |
3 |
Correct |
98 ms |
22988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
17232 KB |
Output is correct |
2 |
Correct |
143 ms |
26456 KB |
Output is correct |
3 |
Correct |
207 ms |
34388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
181 ms |
20508 KB |
Output is correct |
2 |
Correct |
274 ms |
38480 KB |
Output is correct |
3 |
Correct |
209 ms |
33364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
430 ms |
31056 KB |
Output is correct |
2 |
Correct |
303 ms |
40680 KB |
Output is correct |
3 |
Correct |
459 ms |
52292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
15700 KB |
Output is correct |
2 |
Correct |
317 ms |
41276 KB |
Output is correct |
3 |
Correct |
324 ms |
46260 KB |
Output is correct |
4 |
Correct |
668 ms |
64236 KB |
Output is correct |