이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <set>
#include <map>
using namespace std;
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vb;
typedef set<ll> sll;
typedef map<ll, ll> mll_ll;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define sz(a) (ll) a.size()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define trav(i, a, t) for (t::iterator i = a.begin(); i != a.end(); i++)
ll n, m;
vvpll g; // (neigh, index of cur in g[neigh])
vvll answ;
vb onS;
ll DFS(ll cur, ll par) {
onS[cur] = true;
ll res = cur;
while (res == cur) {
if (g[cur].empty()) return par;
pll nxt = g[cur].back();
g[cur].pop_back();
while (sz(g[nxt.first]) <= nxt.second) { // while we have already used edge (cur, nxt)
if (g[cur].empty()) return par;
nxt = g[cur].back();
g[cur].pop_back();
}
if (onS[nxt.first]) {
// cycle time boiii!!
answ.pb(vll());
answ.back().pb(nxt.first);
onS[cur] = false;
answ.back().pb(cur);
return nxt.first;
}
res = DFS(nxt.first, cur);
}
// now res != cur
answ.back().pb(cur);
onS[cur] = false;
return res;
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> m;
g.resize(n);
rep(i, 0, m) {
ll tmpu, tmpv;
cin >> tmpu >> tmpv;
tmpu--; tmpv--;
g[tmpu].pb(mp(tmpv, sz(g[tmpv])));
g[tmpv].pb(mp(tmpu, sz(g[tmpu]) - 1));
}
onS.resize(n, false);
rep(i, 0, n) {
DFS(i, -1);
}
trav(itr, answ, vvll) {
trav(itr2, (*itr), vll) {
cout << (*itr2) + 1 << " ";
}
cout << "\n";
}
cout << flush;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |