提出 #62561474


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int, int>;
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define v vector
constexpr int N = 2e5 + 5;

vi components;
v<vi> edges;
v<pii> adj[N];
bool vis[N];

void dfs(int p, int u, int comp) {
  vis[u] = 1;

  for(const auto& e : adj[u]) {
    if(e.first != p) {
      if(vis[e.first]){
        edges.push_back({comp, e.first, e.second});
      } else {
        dfs(u, e.first, comp);
      }
    }
  }
}

void solve(){
  int n, m;
  cin >> n >> m;

  rep(i, 0, m){
    int a, b; cin >> a >> b;
    adj[a].push_back({b, i + 1});
    if(a != b){
      adj[b].push_back({a, i + 1});
    }
  }


  rep(i, 1, n + 1){
    if(!vis[i]){
      components.push_back(i);
      dfs(-1, i, i);
    }
  }


  
  cout << sz(components) - 1 << '\n';
  if(sz(components) > 1){

    queue<int> left;
    auto edge = edges.back();

    rep(i, 0, sz(components)){
      if(components[i] != edge[0]){
        left.push(components[i]);
      }
    }

    while(sz(left)){
      edge = edges.back();
      if(left.front() == edge[2]){
        left.push(left.front());
        left.pop();
        continue;
      }

      cout << edge[2] << ' ' << edge[1] << ' ' << left.front() << '\n';
      left.pop();
      edges.pop_back();
    }


  }

}

int main(){
  // ios_base::sync_with_stdio(false), cin.tie(NULL);
  // int t; cin >> t; while(t--)
  solve();
  return 0;
}

提出情報

提出日時
問題 E - Cables and Servers
ユーザ d19n
言語 C++ 20 (gcc 12.2)
得点 0
コード長 1667 Byte
結果 WA
実行時間 126 ms
メモリ 36592 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 450
結果
AC × 3
AC × 13
WA × 9
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, sample_01.txt, sample_02.txt, sample_03.txt
ケース名 結果 実行時間 メモリ
random_01.txt WA 70 ms 18992 KiB
random_02.txt AC 50 ms 26348 KiB
random_03.txt WA 6 ms 4684 KiB
random_04.txt WA 2 ms 3612 KiB
random_05.txt AC 97 ms 32512 KiB
random_06.txt AC 51 ms 26432 KiB
random_07.txt WA 9 ms 5052 KiB
random_08.txt WA 2 ms 3532 KiB
random_09.txt WA 76 ms 19628 KiB
random_10.txt AC 33 ms 15196 KiB
random_11.txt WA 99 ms 20748 KiB
random_12.txt WA 2 ms 3548 KiB
random_13.txt AC 110 ms 15896 KiB
random_14.txt AC 121 ms 36592 KiB
random_15.txt AC 105 ms 15984 KiB
random_16.txt AC 121 ms 28960 KiB
random_17.txt WA 126 ms 20624 KiB
random_18.txt AC 113 ms 18704 KiB
random_19.txt AC 2 ms 3564 KiB
sample_01.txt AC 2 ms 3508 KiB
sample_02.txt AC 2 ms 3508 KiB
sample_03.txt AC 2 ms 3464 KiB