Pagini recente » Cod sursa (job #2338960) | Cod sursa (job #2735928) | Cod sursa (job #2171425) | Cod sursa (job #2510005) | Cod sursa (job #3262051)
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <stack>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int DIM = 5e5 + 5;
const ll INF = 1e18;
bool cyc[DIM];
int viz[DIM];
int nr;
int low[DIM], lim[DIM];
int wh[DIM];
vector<pair<int, int>> v[DIM];
vector<pair<int, int>> vt[DIM];
vector<vector<int>> ctc;
stack<int> st;
void dfs(int nod) {
viz[nod] = 1;
low[nod] = lim[nod] = ++nr;
st.push(nod);
for (auto it : v[nod]) {
if (!viz[it.first]) {
dfs(it.first);
low[nod] = min(low[nod], low[it.first]);
} else if (viz[it.first] == 1) {
low[nod] = min(low[nod], lim[it.first]);
}
}
if (low[nod] == lim[nod]) {
ctc.emplace_back();
int x;
do {
x = st.top();
st.pop();
viz[x] = 2;
wh[x] = ctc.size();
ctc.back().push_back(x);
} while (x != nod);
}
}
void dfs2(int nod) {
viz[nod] = 3;
for (auto it : v[nod]) {
if (viz[it.first] < 3) {
dfs2(it.first);
}
cyc[nod] |= cyc[it.first];
}
viz[nod] = 4;
}
int f[DIM];
ll sum[DIM];
bool dfs3(int nod) {
f[nod] = 1;
for (auto it : vt[nod]) {
if (wh[it.first] != wh[nod])
continue;
if (f[it.first]) {
if (sum[it.first] != sum[nod] + it.second)
return true;
} else {
sum[it.first] = sum[nod] + it.second;
bool r = dfs3(it.first);
if (r)
return true;
}
}
f[nod] = 2;
return false;
}
void solve() {
int n, m, q;
cin >> n >> m;
//cin >> n >> m >> q;
for (int i = 1; i <= m; ++i) {
int a, b, x, y;
cin >> a >> b;
a--, b--;
v[a].push_back({b, 1});
continue;
if (b == 0)
continue;
x = (a % n + n) % n;
y = ((a + b) % n + n) % n;
v[x].push_back({y, b});
vt[x].push_back({y, b});
vt[y].push_back({x, -b});
}
for (int i = 0; i < n; ++i) {
if (!viz[i]) {
dfs(i);
}
}
for (int i = 0; i < n; ++i) {
assert(viz[i] == 2);
}
cout << ctc.size() << '\n';
for (auto &c : ctc) {
for (auto nod : c) {
cout << nod + 1<< ' ';
}
cout << '\n';
}
return;
for (int i = 0; i < n; ++i) {
if (viz[i] < 3)
dfs2(i);
}
for (int i = 0; i < n; ++i) {
assert(viz[i] == 4);
}
for (int i = 1; i <= q; ++i) {
int x;
cin >> x;
x = (x % n + n) % n;
if (cyc[x]) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
}
signed main() {
#ifdef LOCAL
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#else
freopen("ctc.in", "r", stdin);
freopen("ctc.out", "w", stdout);
#endif // LOCAL
ios_base::sync_with_stdio(false);
cin.tie(0);
int nrt = 1;
//cin >> nrt;
for (int t = 1; t <= nrt; t++) {
solve();
}
return 0;
}