This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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());
for(int i = 0; i < b.size(); i++) {
if(ok[b[i].se] == 1) order.push_back({b[i].fi,{b[i].se,i}});
}
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 (stderr)
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:83: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]
83 | for(int i = 0; i < b.size(); i++) {
| ~~^~~~~~~~~~
tra.cpp:89: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]
89 | for(int i = 0; i < order.size(); i++) {
| ~~^~~~~~~~~~~~~~
# | 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... |