Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef DEBUG
- #define _GLIBCXX_DEBUG
- #endif
- #include <bits/stdc++.h>
- using namespace std;
- typedef long double ld;
- #ifdef DEBUG
- #define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
- #else
- #define eprintf(...) ;
- #endif
- #define sz(x) ((int) (x).size())
- #define TASK "text"
- const int inf = (int) 1.01e9;
- const long long infll = (long long) 1.01e18;
- const ld eps = 1e-9;
- const ld pi = acos((ld) -1);
- #ifdef DEBUG
- mt19937 mrand(300);
- #else
- mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
- #endif
- int rnd(int x) {
- return mrand() % x;
- }
- void precalc() {
- }
- const int maxn = (int) 1e5 + 5;
- int n, m;
- vector<pair<int, int>> g[maxn];
- bool read() {
- if (scanf("%d%d", &n, &m) < 2) {
- return false;
- }
- for (int i = 0; i < n; i++) {
- g[i].clear();
- }
- for (int i = 0; i < m; i++) {
- int v, u;
- scanf("%d%d", &v, &u);
- v--;
- u--;
- g[u].push_back(make_pair(v, i));
- }
- return true;
- }
- int used[maxn];
- int p[maxn], q[maxn];
- int pid[maxn];
- int perm[maxn];
- bool dfs(int v) {
- used[v] = true;
- for (int i = 0; i < sz(g[v]); i++) {
- int u = g[v][i].first;
- if (p[u] == -1) {
- p[u] = v;
- q[v] = u;
- return true;
- }
- }
- for (int i = 0; i < sz(g[v]); i++) {
- int u = g[v][i].first;
- if (!used[p[u]] && dfs(p[u])) {
- p[u] = v;
- q[v] = u;
- return true;
- }
- }
- return false;
- }
- int ans[maxn];
- int dfs1(int v) {
- used[v] = true;
- int res = 1;
- for (int i = 0; i < sz(g[v]); i++) {
- int u = g[v][i].first, id = g[v][i].second;
- if (used[p[u]]) {
- continue;
- }
- int cur = dfs1(p[u]);
- ans[pid[u]] -= cur;
- ans[id] += cur;
- res += cur;
- }
- return res;
- }
- void solve() {
- for (int i = 0; i < n; i++) {
- p[i] = -1;
- q[i] = -1;
- perm[i] = i;
- shuffle(g[i].begin(), g[i].end(), mrand);
- }
- shuffle(perm, perm + n, mrand);
- while (true) {
- bool found = false;
- for (int i = 0; i < n; i++) {
- used[i] = false;
- }
- for (int i = 0; i < n; i++) {
- int v = perm[i];
- if (v == 0) {
- continue;
- }
- if (q[v] == -1 && !used[v] && dfs(v)) {
- found = true;
- }
- }
- if (!found) {
- break;
- }
- }
- for (int i = 0; i < m; i++) {
- ans[i] = 0;
- }
- for (int v = 1; v < n; v++) {
- if (q[v] == -1) {
- printf("-1\n");
- return;
- }
- for (int i = 0; i < sz(g[v]); i++) {
- int u = g[v][i].first, id = g[v][i].second;
- if (u == q[v]) {
- ans[id] += n;
- pid[u] = id;
- }
- }
- }
- for (int i = 0; i < n; i++) {
- used[i] = false;
- }
- dfs1(0);
- for (int i = 0; i < n; i++) {
- if (!used[i]) {
- printf("-1\n");
- }
- }
- for (int i = 0; i < m; i++) {
- printf("%d\n", ans[i]);
- }
- }
- int main() {
- precalc();
- #ifdef DEBUG
- assert(freopen(TASK ".in", "r", stdin));
- assert(freopen(TASK ".out", "w", stdout));
- #endif
- while (read()) {
- solve();
- #ifdef DEBUG
- eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
- #endif
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement