#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 3e5;
struct Point { int x, y; };
int N, M, A, B;
Point P[MAXN+10];
vector<int> adj[MAXN+10], revadj[MAXN+10], adj2[MAXN+10];
int scc[MAXN+10];
bool vis[MAXN+10];
vector<int> order;
struct Data
{
int l, r, num;
Data() : l(-1), r(-1), num(0) {}
Data(int l, int r, int num) : l(l), r(r), num(num) {}
};
Data operator + (const Data &p, const Data &q)
{
if(p.num==0) return q;
if(q.num==0) return p;
if(p.l==q.l && p.r==q.r) return {p.l, p.r, max(p.num, q.num)};
Data ret;
ret.num=p.num+q.num;
ret.l=min(p.l, q.l);
ret.r=max(p.r, q.r);
if(p.l==q.r || q.l==p.r) ret.num--;
return ret;
}
Data dp[MAXN+10];
void dfs(int now)
{
vis[now]=true;
for(auto nxt : adj[now]) if(!vis[nxt]) dfs(nxt);
order.push_back(now);
}
void dfs2(int now, int col)
{
scc[now]=col;
for(auto nxt : revadj[now]) if(!scc[nxt]) dfs2(nxt, col);
}
int main()
{
int i, j;
scanf("%d%d%d%d", &N, &M, &A, &B);
for(i=1; i<=N; i++) scanf("%d%d", &P[i].x, &P[i].y);
for(i=1; i<=M; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
adj[u].push_back(v), revadj[v].push_back(u);
if(w==2) adj[v].push_back(u), revadj[u].push_back(v);
}
for(i=1; i<=N; i++) if(!vis[i]) dfs(i);
reverse(order.begin(), order.end());
int cnt=0;
for(auto now : order)
{
if(scc[now]) continue;
dfs2(now, ++cnt);
}
for(i=1; i<=N; i++)
{
int now=i;
for(auto nxt : adj[now])
{
if(scc[now]==scc[nxt]) continue;
adj2[scc[now]].push_back(scc[nxt]);
}
}
//for(i=1; i<=N; i++) printf("%d : %d\n", i, scc[i]);
int p=cnt;
vector<pii> t;
for(i=1; i<=N; i++) if(P[i].x==A)
{
adj2[scc[i]].push_back(++p);
dp[p]={P[i].y, P[i].y, 1};
}
for(i=1; i<=cnt; i++)
{
sort(adj2[i].begin(), adj2[i].end());
adj2[i].erase(unique(adj2[i].begin(), adj2[i].end()), adj2[i].end());
}
for(i=cnt; i>=1; i--)
{
int now=i;
sort(adj2[now].begin(), adj2[now].end(), [&](const int &p, const int &q) { return pii(dp[p].l, dp[p].r)<pii(dp[q].l, dp[q].r); });
//printf("%d -> ", i);
//for(auto nxt : adj2[i]) printf("%d ", nxt);
//printf("\n");
for(auto nxt : adj2[now]) dp[now]=dp[now]+dp[nxt];
//printf("%d : %d %d %d\n", now, dp[now].l, dp[now].r, dp[now].num);
}
vector<pii> ans;
for(i=1; i<=N; i++) if(P[i].x==0) ans.push_back({P[i].y, dp[scc[i]].num});
sort(ans.begin(), ans.end(), greater<pii>());
for(auto it : ans) printf("%d\n", it.second);
}
Compilation message
tra.cpp: In function 'int main()':
tra.cpp:57:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
tra.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &N, &M, &A, &B);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
tra.cpp:60:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf("%d%d", &P[i].x, &P[i].y);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
tra.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &u, &v, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24956 KB |
Output is correct |
2 |
Correct |
25 ms |
24952 KB |
Output is correct |
3 |
Correct |
28 ms |
24952 KB |
Output is correct |
4 |
Correct |
25 ms |
24952 KB |
Output is correct |
5 |
Correct |
25 ms |
24952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24964 KB |
Output is correct |
2 |
Correct |
25 ms |
24952 KB |
Output is correct |
3 |
Correct |
25 ms |
25080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
24952 KB |
Output is correct |
2 |
Correct |
25 ms |
25108 KB |
Output is correct |
3 |
Incorrect |
26 ms |
25080 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
25208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
26948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
29600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
151 ms |
34360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
234 ms |
36392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
347 ms |
43716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
514 ms |
55112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1118 ms |
68460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
326 ms |
86728 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |