This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In the name of God
//#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MXN = 3e5 + 10;
ll n, m, h, w, ts;
ll te[MXN], tw[MXN], dp[MXN], cmp[MXN], comp[MXN];
vector<ll> adj[MXN], adt[MXN], Top, G[MXN], C[MXN];
bool east[MXN], west[MXN], vis[MXN], is[MXN];
vector<pair<ll, ll>> ANS;
inline void add_edge(ll u, ll v){
adj[u].push_back(v);
adt[v].push_back(u);
}
void dfs(ll u){
vis[u] = 1;
for(auto v : adj[u]){
if(!vis[v]) dfs(v);
}
Top.push_back(u);
}
void sfd(ll u, ll c){
comp[u] = c, C[c].push_back(u);
for(auto v : adt[u]){
if(!comp[v]) sfd(v, c);
}
}
ll DFS(ll u){
ll res = te[u];
vis[u] = 1;
for(auto v : G[u]){
if(!vis[v]) res += DFS(v);
}
return res;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
cin >> n >> m >> h >> w;
for(int i = 1; i <= n; i ++){
ll x, y; cin >> x >> y;
east[i] = (x == h), west[i] = (x == 0);
cmp[i] = -y;
}
for(int i = 1; i <= m; i ++){
ll u, v, f; cin >> u >> v >> f;
add_edge(u, v);
if(f > 1) add_edge(v, u);
}
for(int i = 1; i <= n; i ++){
if(!vis[i]) dfs(i);
}
reverse(Top.begin(), Top.end());
for(int j = 1; j <= n; j ++){
ll i = Top[j - 1];
if(!comp[i]) sfd(i, ++ ts);
}
for(int i = 1; i <= n; i ++){
te[comp[i]] += east[i];
tw[comp[i]] += west[i];
}
for(int i = 1; i <= ts; i ++){
for(auto u : C[i]){
for(auto v : adj[u]){
if(comp[v] != i && !is[comp[v]]){
is[comp[v]] = 1;
G[i].push_back(comp[v]);
}
}
}
for(auto X : G[i]) is[X] = 0;
}
/*
cout << ts << '\n';
cout << "comps : \n";
for(int i = 1; i <= n; i ++){
cout << comp[i] << ' ';
} cout << '\n';
cout << "edges of scc : \n";
for(int i = 1; i <= ts; i ++){
cout << "scc " << i << " : \n";
cout << "te = " << te[i] << '\n';
for(auto j : G[i]) cout << j << ' ';
cout << '\n';
}
*/
for(int i = ts; i; -- i){
//dp[i] = te[i];
//for(auto j : G[i]) dp[i] += dp[j];
fill(vis, vis + ts + 1, 0);
dp[i] = DFS(i);
}
for(int i = 1; i <= n; i ++){
if(west[i]){
ANS.push_back({cmp[i], dp[comp[i]]});
}
}
sort(ANS.begin(), ANS.end());
for(auto X : ANS) cout << X.second << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |