QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#815764#9875. Don't Detect Cycleucup-team133#TL 1679ms4488kbC++239.1kb2024-12-15 16:59:142024-12-15 16:59:15

Judging History

你现在查看的是最新测评结果

  • [2024-12-15 16:59:15]
  • 评测
  • 测评结果:TL
  • 用时:1679ms
  • 内存:4488kb
  • [2024-12-15 16:59:14]
  • 提交

answer

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <random>
#include <stdio.h>
#include <fstream>
#include <functional>
#include <cassert>
#include <unordered_map>
#include <bitset>
#include <chrono>





using namespace std;




#define rep(i,n) for (int i=0;i<n;i+=1)
#define rrep(i,n) for (int i=n-1;i>-1;i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()

#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << " )\n";

template<class T>
using vec = vector<T>;
template<class T>
using vvec = vec<vec<T>>;
template<class T>
using vvvec = vec<vvec<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;


template<class T>
bool chmin(T &a, T b){
  if (a>b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmax(T &a, T b){
  if (a<b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
T sum(vec<T> x){
  T res=0;
  for (auto e:x){
    res += e;
  }
  return res;
}

template<class T>
void printv(vec<T> x){
  for (auto e:x){
    cout<<e<<" ";
  }
  cout<<endl;
}



template<class T,class U>
ostream& operator<<(ostream& os, const pair<T,U>& A){
  os << "(" << A.first <<", " << A.second << ")";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const set<T>& S){
  os << "set{";
  for (auto a:S){
    os << a;
    auto it = S.find(a);
    it++;
    if (it!=S.end()){
      os << ", ";
    }
  }
  os << "}";
  return os;
}

template<class T0,class T1,class T2>
ostream& operator<<(ostream& os, const tuple<T0,T1,T2>& a){
  auto [x,y,z] = a;
  os << "(" << x << ", " << y << ", " << z << ")";
  return os;
}

template<class T0,class T1,class T2,class T3>
ostream& operator<<(ostream& os, const tuple<T0,T1,T2,T3>& a){
  auto [x,y,z,w] = a;
  os << "(" << x << ", " << y << ", " << z << ", " << w  << ")";
  return os;
}

template<class T,class U>
ostream& operator<<(ostream& os, const map<U,T>& A){
  os << "map{";
  for (auto e:A){
    os << e.first;
    os << ":";
    os << e.second;
    os << ", ";
  }
  os << "}";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const vec<T>& A){	
  os << "[";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ", ";
    }
  }
  os << "]" ;
  return os;
}

struct LowLink {
    int n, m = 0, t = 0, b = 0;
    std::vector<std::vector<int>> G;
    std::vector<std::pair<int, int>> es;
    std::vector<int> ord, low, tecc, bcc, tmp;
    std::vector<bool> is_articulation, is_bridge, used;

    LowLink(int n) : n(n), G(n), ord(n, -1), low(n), tecc(n, -1), is_articulation(n, false) {}

    void add_edge(int u, int v) {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        G[u].emplace_back(m);
        G[v].emplace_back(m);
        es.emplace_back(u, v);
        is_bridge.emplace_back(false);
        bcc.emplace_back();
        used.emplace_back(false);
        m++;
    }

    void build() {
        for (int i = 0; i < n; i++) {
            if (ord[i] != -1) continue;
            dfs1(i, 0, -1);
            dfs2(i, -1);
        }
    }

    std::pair<int, int> operator[](int k) const { return es[k]; }

  private:
    void dfs1(int v, int k, int pre) {
        ord[v] = k++, low[v] = ord[v];
        int cnt = 0;
        for (int& e : G[v]) {
            if (e == pre) continue;
            int u = es[e].first ^ es[e].second ^ v;
            if (ord[u] == -1) {
                cnt++;
                used[e] = true;
                dfs1(u, k, e);
                low[v] = std::min(low[v], low[u]);
                if (pre != -1 and ord[v] <= low[u]) is_articulation[v] = true;
                if (ord[v] < low[u]) is_bridge[e] = true;
            } else
                low[v] = std::min(low[v], ord[u]);
        }
        if (pre == -1 and cnt > 1) is_articulation[v] = true;
    }

    void dfs2(int v, int pre) {
        if (pre == -1) tecc[v] = t++;
        for (int& e : G[v]) {
            if (e == pre) continue;
            int u = es[e].first ^ es[e].second ^ v;
            if (tecc[u] == -1 or ord[u] < ord[v]) tmp.emplace_back(e);
            if (tecc[u] >= 0) continue;
            if (ord[v] >= low[u])
                tecc[u] = tecc[v];
            else
                tecc[u] = t++;
            dfs2(u, e);
            if (ord[v] <= low[u]) {
                while (true) {
                    int ne = tmp.back();
                    tmp.pop_back();
                    bcc[ne] = b;
                    if (ne == e) break;
                }
                b++;
            }
        }
    }
};



void solve(){

  int N,M;
  cin>>N>>M;
  vec<pair<int,int>> Edge(M);
  rep(i,M){
    int u,v;
    cin>>u>>v;
    Edge[i] = {u-1,v-1};
  }

  vec<int> rest_edge(M,1);

  

  auto search_nxt_edge = [&](){
    LowLink G(N);
    vec<int> rest_eids;
    rep(i,M){
      if (rest_edge[i]){
        rest_eids.push_back(i);
        G.add_edge(Edge[i].first,Edge[i].second);
      }
    }

    G.build();

    vec<vec<pair<int,int>>> tmp_dfstree(N);
    vec<vec<pair<int,int>>>tmp_back(N);
    rep(i,rest_eids.size()){
      if (G.used[i]){
        auto [u,v] = Edge[rest_eids[i]];
        tmp_dfstree[u].push_back({v,i});
        tmp_dfstree[v].push_back({u,i});
      }
      else{
        auto [u,v] = Edge[rest_eids[i]];
        tmp_back[u].push_back({v,i});
        tmp_back[v].push_back({u,i});
      }
    }

    vec<int> dep(N,-1);
    vec<pair<int,int>> parent(N,{-1,-1});
    vec<int> topo;
    rep(root,N){
      if (dep[root]!=-1) continue;
      dep[root] = 0;
      deque<int> deq = {root};
      while (!deq.empty()){
        auto v = deq.front(); deq.pop_front();
        topo.push_back(v);
        for (auto [nv,gomi]:tmp_dfstree[v]){
          if (dep[nv] == -1){
            parent[nv] = {v,gomi};
            dep[nv] = dep[v] + 1;
            deq.push_back(nv);
          }
        }
      }
    }

    vec<int> back_edge_add(N,0);
    rep(i,rest_eids.size()){
      if (!G.used[i]){
        auto [u,v] = Edge[rest_eids[i]];
        if (dep[u] > dep[v]) swap(u,v);
        back_edge_add[v]++;
        if (parent[u].first!=-1){
          back_edge_add[parent[u].first] -= 1;
        }
      }
    }

    reverse(all(topo));
    for (auto v:topo){
      for (auto [nv,gomi]:tmp_dfstree[v]){
        if (nv == parent[v].first) continue;
        back_edge_add[v] += back_edge_add[nv];
      }
    }

    rep(i,rest_eids.size()){
      if (!G.used[i]){
        auto [u,v] = Edge[rest_eids[i]];
        if (back_edge_add[u] == 1 && back_edge_add[v] == 1){
          return rest_eids[i];
        }
      }
    }
    //debug(tmp_dfstree);

    vec<int> removal_edge(rest_eids.size(),0);

    vec<int> bcc_cnt(N,0);
    vec<vec<int>> tmp_bcc(N);

    rep(v,N){
      int tmp_max_size = 0;
      int tmp_double_size = 0;
      for (auto [nv,eid_in_G]:tmp_dfstree[v]){
        int bcc_id = G.bcc[eid_in_G];
        bcc_cnt[bcc_id]++;
        if (bcc_cnt[bcc_id] == 2){
          tmp_double_size++;
        }
        chmax(tmp_max_size,bcc_cnt[bcc_id]);
        tmp_bcc[bcc_id].push_back(eid_in_G);
      }

      for (auto [nv,eid_in_G]:tmp_back[v]){
        int bcc_id = G.bcc[eid_in_G];
        bcc_cnt[bcc_id]++;
        if (bcc_cnt[bcc_id] == 2){
          tmp_double_size++;
        }
        chmax(tmp_max_size,bcc_cnt[bcc_id]);
        tmp_bcc[bcc_id].push_back(eid_in_G);
      }

      if (tmp_max_size <= 2 && tmp_double_size <= 1){
        for (auto [nv,eid_in_G]:tmp_dfstree[v]){
          int bcc_id = G.bcc[eid_in_G];
          if (bcc_cnt[bcc_id] == 1){
            if (tmp_double_size == 0){
              removal_edge[eid_in_G] += (v + 1);
            }
          }
          else{
            removal_edge[eid_in_G] += (v + 1);
          }
        }
      }

      for (auto [nv,eid_in_G]:tmp_dfstree[v]){
        int bcc_id = G.bcc[eid_in_G];
        bcc_cnt[bcc_id]--;
        tmp_bcc[bcc_id].clear();
      }

      for (auto [nv,eid_in_G]:tmp_back[v]){
        int bcc_id = G.bcc[eid_in_G];
        bcc_cnt[bcc_id]--;
        tmp_bcc[bcc_id].clear();
      }
    }

    rep(i,rest_eids.size()){
      auto [u,v] = Edge[rest_eids[i]];
      if (removal_edge[i] == u + v + 2){
        return rest_eids[i];
      }
    }

    return -1;
    
  };

  vec<int> res;

  rep(_,M){
    int remove_idx = search_nxt_edge();
    if (remove_idx == -1){
      cout << -1 << "\n";
      return ;
    }
    res.push_back(remove_idx+1);
    rest_edge[remove_idx] = 0;
  }

  reverse(all(res));
  rep(i,M){
    cout << res[i];
    if (i == M-1){
      cout << "\n";
    }
    else{
      cout << " ";
    }
  }



  

  



  


  
}






int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  cout << setprecision(15);

  int T;
  cin>>T;
  while (T--){
    solve();
  }
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3564kb

input:

1
4 4
1 2
2 3
3 4
4 2

output:

3 2 1 4

result:

ok Correct

Test #2:

score: 0
Accepted
time: 1ms
memory: 3644kb

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 7 6 4 2 8 5 3 1
-1

result:

ok Correct

Test #3:

score: 0
Accepted
time: 1679ms
memory: 4488kb

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:

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 2871 2870 2869 2868 2867 2866 2865 2864 2863 2862 2861 2860 2859 2858 2857 2856 2855 2854 2853 2852 2851 2850 2849 2848 2847 ...

result:

ok Correct

Test #4:

score: 0
Accepted
time: 34ms
memory: 4044kb

input:

48
732 104
388 425
176 558
7 695
504 507
163 705
204 456
139 432
104 716
535 582
254 682
70 278
77 385
600 680
373 564
197 653
335 569
81 579
339 604
407 580
253 383
480 549
145 308
52 373
426 525
268 359
408 595
47 397
479 569
268 403
477 663
434 660
330 343
56 692
376 450
200 553
299 713
114 584
1...

output:

104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 ...

result:

ok Correct

Test #5:

score: 0
Accepted
time: 1317ms
memory: 4356kb

input:

24
3635 2454
724 2161
994 3233
30 278
2047 3627
693 1048
112 2609
9 1552
889 946
987 2538
923 1911
53 1198
2429 3200
1338 3544
504 2644
1116 3446
815 877
245 3601
2177 3180
212 1638
1140 3241
159 2455
2447 2460
957 1585
980 2338
1254 3014
382 3596
510 595
1408 2300
2053 2276
2177 3415
1051 3353
136 ...

output:

2454 2453 2452 2451 2450 2449 2448 2447 2446 2445 2444 2443 2442 2441 2440 2438 2437 2436 2435 2434 2433 2432 2431 2430 2429 2428 2427 2426 2425 2424 2423 2422 2421 2420 2419 2418 2417 2416 2415 2414 2413 2412 2411 2410 2409 2408 2407 2406 2405 2404 2403 2402 2401 2400 2399 2398 2397 2396 2395 2394 ...

result:

ok Correct

Test #6:

score: 0
Accepted
time: 647ms
memory: 4180kb

input:

56
2367 1768
132 2148
1280 2214
473 2270
78 2126
374 2080
777 1617
74 152
46 125
36 1136
1340 2010
1536 1801
291 619
610 1567
1688 2303
1005 2308
1101 1988
1695 2257
1056 1405
1134 1579
1819 2281
1281 1952
2065 2102
1984 2353
215 1994
984 2258
1916 2059
1128 2198
966 1048
965 1424
866 932
227 543
33...

output:

1768 1767 1766 1765 1764 1763 1762 1761 1760 1759 1758 1757 1756 1755 1754 1753 1752 1751 1750 1749 1748 1747 1746 1745 1744 1743 1742 1741 1740 1739 1738 1737 1736 1735 1734 1733 1732 1731 1730 1729 1728 1727 1726 1725 1724 1723 1722 1721 1720 1719 1718 1717 1716 1714 1713 1712 1711 1710 1709 1708 ...

result:

ok Correct

Test #7:

score: 0
Accepted
time: 408ms
memory: 4180kb

input:

56
1804 2031
215 520
41 228
505 1449
1202 1467
175 474
583 1684
127 1013
11 1132
251 1009
1333 1516
22 633
168 1160
866 1584
1501 1510
425 1494
563 1764
1341 1646
76 114
541 943
163 166
103 184
455 1225
708 1649
836 1551
551 1381
570 1509
125 221
371 1117
436 1012
392 732
76 379
1040 1359
119 1405
1...

output:

-1
17 16 15 14 13 12 11 10 9 7 6 4 1 18 5 3 8 2
18 17 13 12 11 10 9 6 2 1 16 4 19 15 8 7 5 3 14 20
22 21 20 19 17 14 12 11 10 9 5 3 23 18 16 15 13 8 7 6 4 2 25 1 24
21 20 19 18 17 16 15 4 2 1 13 14 11 9 8 7 6 5 3 10 12
20 19 18 17 14 13 11 10 8 7 6 3 2 16 9 5 4 1 12 15
20 19 17 16 15 14 12 9 7 4 3 1...

result:

ok Correct

Test #8:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

38
17 122
7 11
1 8
2 13
5 6
6 10
9 17
6 13
10 12
2 9
12 14
14 15
3 8
8 12
3 16
3 17
6 16
5 12
4 11
11 16
5 13
5 17
1 4
1 10
8 15
2 16
3 10
6 7
5 7
2 17
10 17
7 12
3 6
9 11
6 17
4 6
9 16
1 16
12 15
7 17
9 10
1 5
10 15
7 10
3 13
1 14
8 14
4 5
4 17
1 17
8 17
7 8
1 2
10 13
11 15
15 16
2 12
2 11
3 7
8 9
...

output:

-1
-1
-1
-1
-1
17 16 15 14 13 12 11 10 9 7 6 4 1 18 5 3 8 2
18 17 13 12 11 10 9 6 2 1 16 4 19 15 8 7 5 3 14 20
22 21 20 19 17 14 12 11 10 9 5 3 23 18 16 15 13 8 7 6 4 2 25 1 24
21 20 19 18 17 16 15 4 2 1 13 14 11 9 8 7 6 5 3 10 12
20 19 18 17 14 13 11 10 8 7 6 3 2 16 9 5 4 1 12 15
20 19 17 16 15 14 ...

result:

ok Correct

Test #9:

score: 0
Accepted
time: 5ms
memory: 3984kb

input:

61
12 66
11 12
5 8
9 12
4 9
2 9
6 12
2 11
1 2
3 6
3 12
6 10
5 6
2 12
10 12
8 12
7 8
7 9
2 8
3 11
3 9
3 10
8 11
2 6
5 12
5 9
4 7
4 5
4 6
5 11
1 3
5 7
1 7
7 10
5 10
6 7
4 12
3 5
4 8
2 3
1 8
6 11
4 11
3 7
1 5
3 4
9 11
1 10
4 10
6 9
7 11
1 4
8 9
10 11
1 11
7 12
1 9
9 10
1 12
6 8
8 10
2 10
2 5
3 8
2 7
1 ...

output:

-1
-1
-1
-1
-1
-1
17 16 15 14 13 12 11 10 9 7 6 4 1 18 5 3 8 2
18 17 13 12 11 10 9 6 2 1 16 4 19 15 8 7 5 3 14 20
22 21 20 19 17 14 12 11 10 9 5 3 23 18 16 15 13 8 7 6 4 2 25 1 24
21 20 19 18 17 16 15 4 2 1 13 14 11 9 8 7 6 5 3 10 12
20 19 18 17 14 13 11 10 8 7 6 3 2 16 9 5 4 1 12 15
20 19 17 16 15 ...

result:

ok Correct

Test #10:

score: 0
Accepted
time: 2ms
memory: 3688kb

input:

18
51 1255
24 43
42 51
4 36
29 31
41 42
43 48
10 26
30 40
4 51
25 42
24 42
2 6
3 24
6 21
34 46
5 10
2 37
12 41
19 25
1 2
18 22
1 20
45 49
3 22
14 25
16 25
26 31
25 48
36 45
24 29
34 39
26 29
6 37
18 38
2 51
10 22
15 26
30 33
1 15
10 37
17 33
11 22
28 32
32 39
13 17
21 28
8 23
20 46
8 38
5 44
5 30
4 ...

output:

-1
-1
-1
-1
-1
-1
-1
-1
17 16 15 14 13 12 11 10 9 7 6 4 1 18 5 3 8 2
18 17 13 12 11 10 9 6 2 1 16 4 19 15 8 7 5 3 14 20
22 21 20 19 17 14 12 11 10 9 5 3 23 18 16 15 13 8 7 6 4 2 25 1 24
21 20 19 18 17 16 15 4 2 1 13 14 11 9 8 7 6 5 3 10 12
20 19 18 17 14 13 11 10 8 7 6 3 2 16 9 5 4 1 12 15
20 19 17 ...

result:

ok Correct

Test #11:

score: 0
Accepted
time: 5ms
memory: 3704kb

input:

61
22 223
1 22
10 22
2 7
19 20
13 17
17 21
18 19
15 16
9 17
5 19
5 8
12 18
4 17
10 20
2 10
4 15
7 11
16 19
5 20
3 14
3 17
7 12
3 21
4 11
17 22
10 17
8 21
9 20
6 11
2 20
5 7
3 18
9 22
13 22
6 14
14 19
5 12
4 22
2 3
14 17
12 16
7 20
5 10
4 7
4 13
1 19
10 13
1 20
13 19
4 6
11 19
3 11
9 14
8 15
3 16
2 8...

output:

-1
-1
-1
-1
-1
-1
-1
17 16 15 14 13 12 11 10 9 7 6 4 1 18 5 3 8 2
18 17 13 12 11 10 9 6 2 1 16 4 19 15 8 7 5 3 14 20
22 21 20 19 17 14 12 11 10 9 5 3 23 18 16 15 13 8 7 6 4 2 25 1 24
21 20 19 18 17 16 15 4 2 1 13 14 11 9 8 7 6 5 3 10 12
20 19 18 17 14 13 11 10 8 7 6 3 2 16 9 5 4 1 12 15
20 19 17 16 ...

result:

ok Correct

Test #12:

score: -100
Time Limit Exceeded

input:

1
4000 4000
1248 3248
260 3260
344 1017
843 3949
451 1483
275 1413
231 3477
264 940
567 1383
1072 3173
830 3445
437 2322
929 1624
1221 2034
3297 3458
1412 1642
837 2505
1918 3259
554 2070
3630 3807
1217 3188
3149 3199
949 1179
2697 3656
802 2039
2496 3757
1073 2857
765 2310
178 3862
1385 2597
1870 2...

output:


result: