QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#814911 | #9875. Don't Detect Cycle | ucup-team4435# | TL | 0ms | 3640kb | C++23 | 3.9kb | 2024-12-14 22:32:17 | 2024-12-14 22:32:18 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define ar array
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
int Bit(int mask, int b) { return (mask >> b) & 1; }
template<class T>
bool ckmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool ckmax(T &a, const T &b) {
if (b > a) {
a = b;
return true;
}
return false;
}
// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
--l;
while (r - l > 1) {
T mid = l + (r - l) / 2;
if (predicat(mid)) {
r = mid;
} else {
l = mid;
}
}
return r;
}
template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
return FindFirstTrue(l, r, predicat) - 1;
}
const int INFi = 2e9;
const ll INF = 2e18;
const int N = 4e3 + 5;
ll h[N];
vi g[N];
int U[N], V[N];
bool used[N];
bool bad[N];
ll cnt[N];
ll hh[N];
int n;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void dfs(int v, int p) {
used[v] = true;
for(auto &i : g[v]) {
if (i == p) continue;
int u = U[i] ^ V[i] ^ v;
if (used[u]) {
if (h[i] == -1) {
h[i] = rng() % INF;
cnt[v]++;
} else {
cnt[v]--;
}
hh[v] ^= h[i];
continue;
}
dfs(u, i);
hh[v] ^= hh[u];
}
if (p != -1) {
h[p] = hh[v];
if (cnt[v]) {
bad[v] = true;
bad[U[p] ^ V[p] ^ v] = true;
}
}
}
ll who[N];
void build(const vi &e) {
for(auto &i : e) {
g[U[i]].push_back(i);
g[V[i]].push_back(i);
h[i] = -1;
}
rep(i, n) {
bad[i] = false;
used[i] = false;
hh[i] = 0;
cnt[i] = 0;
}
rep(i, n) {
if (!used[i]) {
dfs(i, -1);
}
}
rep(i, n) {
ll col = 0;
for(auto &j : g[i]) {
if (h[j] == 0) continue;
if (!col) col = h[j];
if (h[j] != col) {
col = -1;
break;
}
}
who[i] = col;
}
rep(i, n) {
g[i].clear();
}
}
void solve() {
int m; cin >> n >> m;
rep(i, m) {
cin >> U[i] >> V[i];
U[i]--;
V[i]--;
}
vi e(m);
iota(all(e), 0);
vi ans;
while(!e.empty()) {
build(e);
if (!ans.empty()) {
int i = ans.back();
if (bad[U[i]] || bad[V[i]]) {
e.push_back(ans.back());
ans.pop_back();
continue;
}
}
bool found = false;
rep(j, e.size()) {
int i = e[j];
int u = U[i];
int v = V[i];
ll col = h[i];
if (who[u] != col || who[v] != col) continue;
ans.push_back(i);
found = true;
e.erase(e.begin() + j);
break;
}
if (!found) {
cout << "-1\n";
return;
}
}
reverse(all(ans));
for(auto &i : ans) {
cout << i + 1 << ' ' ;
}
cout << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(8) << fixed;
int t = 1;
cin >> t;
rep(i, t) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3640kb
input:
1 4 4 1 2 2 3 3 4 4 2
output:
4 3 1 2
result:
ok Correct
Test #2:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
4 4 5 1 2 2 3 3 4 3 1 1 4 5 3 1 2 2 3 3 4 9 10 3 5 1 8 5 8 4 9 6 7 7 9 1 2 1 4 2 4 4 6 8 10 1 4 3 8 2 5 3 4 1 5 5 8 2 8 5 7 4 5 3 7
output:
-1 3 2 1 10 9 8 4 2 7 6 5 3 1 -1
result:
ok Correct
Test #3:
score: -100
Time Limit Exceeded
input:
50 3214 2907 970 1929 2860 3033 1322 2296 931 1192 861 2505 831 2469 231 2549 1 2306 1765 1842 999 3171 177 2007 1798 1894 827 3180 673 1738 1163 1573 2213 2781 2766 3200 1663 2197 1797 2281 315 2637 442 2689 558 2874 1520 2591 651 1923 1133 2920 1747 2412 1104 1528 313 2487 632 3124 660 2182 1581 2...
output:
90 8 1194 2907 2906 2905 2904 2903 2902 2901 2900 2899 2898 2897 2896 2895 2894 2893 2892 2891 2890 2889 2888 2887 2886 2885 2884 2883 2882 2881 2880 2879 2878 2877 2876 2875 2874 2873 2872 2871 2870 2869 2868 2867 2866 2865 2864 2863 2862 2861 2860 2859 2858 2857 2856 2855 2854 2853 2852 2851 2850 ...