【題解】Codeforces 723E. One-Way Reform

【題目敘述】http://codeforces.com/contest/723/problem/E

#include <iostream>
#include <set>
using namespace std;
 
int t, n, m, a, b;
set <int> st[205];
 
void dfs(int x){
    while (st[x].size()){
        int nxt = *st[x].begin();
        st[x].erase(nxt);
        st[nxt].erase(x);
        if (x != 0 && nxt != 0) cout << x << " " << nxt << "\n";
        dfs(nxt);
    }
}
 
void solve(){
    cin >> n >> m;
    int ans = 0;
    for (int i = 0; i < m; i++){
        cin >> a >> b;
        st[a].insert(b);
        st[b].insert(a);
    }
    for (int i = 1; i <= n; i++){
        if (st[i].size() % 2 == 1){
            st[i].insert(0);
            st[0].insert(i);
        }
        else ans++;
    }
    cout << ans << "\n";
    for (int i = 1; i <= n; i++){
        if (st[i].size()) dfs(i);
    }
}
 
int main() {
    cin >> t;
    while (t--){
        solve();
    }
}
分享本文 Share with friends