제출 #873770

#제출 시각아이디문제언어결과실행 시간메모리
873770VictorNaboj (COCI22_naboj)C++17
110 / 110
789 ms77296 KiB
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = (b) - 1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)

#define all(x) x.begin(), x.end()
#define sz(x) (ll)x.size()
#define pb push_back
#define debug(x) cout<<#x<<" = "<<x<<endl

#define umap unordered_map
#define uset unordered_set

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;

const ll INF = 1'000'000'007;

ll n,m;
vector<umap<ll,ll>> graph;
vector<ll> value;
vector<pll> order;

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    cin>>n>>m;
    graph.resize(n);

    rep(i,0,m) {
        ll u,v;
        cin>>u>>v;
        --u; --v;
        graph[u].emplace(v,1);
        graph[v].emplace(u,0);
    }

    value.assign(n,0);

    queue<ll> q;
    vector<bool> vis;
    vis.assign(n,0);

    rep(u,0,n) {
        trav(edge,graph[u]) value[u]+=edge.second;
        if(value[u]%sz(graph[u])==0) {
            q.push(u);
            vis[u]=1;
        }
    }

    while(!q.empty()) {
        ll u=q.front();
        q.pop();

        if(sz(graph[u])) order.emplace_back(u,value[u]/sz(graph[u]));
        else order.emplace_back(u,0);

        trav(edge,graph[u]) {
            ll v,w;
            tie(v,w)=edge;
            graph[v].erase({u});
            value[v]-=1-w;


            if(!vis[v]&&(sz(graph[v])==0||value[v]%sz(graph[v])==0)) {
                vis[v]=1;
                q.push(v);
            }
        }
    }

    if(sz(order)!=n) cout<<-1<<endl;
    else {
        cout<<n<<endl;
        reverse(all(order));
        trav(item,order) cout<<item.first+1<<' '<<item.second<<'\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...