結果

問題 No.2301 Namorientation
ユーザー SSRSSSRS
提出日時 2023-05-12 21:29:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 415 ms / 3,000 ms
コード長 1,708 bytes
コンパイル時間 1,752 ms
コンパイル使用メモリ 180,112 KB
実行使用メモリ 23,640 KB
最終ジャッジ日時 2023-08-19 03:56:29
合計ジャッジ時間 17,708 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,504 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 247 ms
14,756 KB
testcase_13 AC 210 ms
13,768 KB
testcase_14 AC 397 ms
22,220 KB
testcase_15 AC 261 ms
16,116 KB
testcase_16 AC 337 ms
19,808 KB
testcase_17 AC 235 ms
15,276 KB
testcase_18 AC 372 ms
20,048 KB
testcase_19 AC 194 ms
12,944 KB
testcase_20 AC 362 ms
19,916 KB
testcase_21 AC 277 ms
15,976 KB
testcase_22 AC 359 ms
21,036 KB
testcase_23 AC 351 ms
20,788 KB
testcase_24 AC 289 ms
18,044 KB
testcase_25 AC 257 ms
18,616 KB
testcase_26 AC 328 ms
23,640 KB
testcase_27 AC 415 ms
22,844 KB
testcase_28 AC 408 ms
22,656 KB
testcase_29 AC 405 ms
22,664 KB
testcase_30 AC 399 ms
22,584 KB
testcase_31 AC 407 ms
22,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N; i++){
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    E[A].push_back(make_pair(i, B));
    E[B].push_back(make_pair(i, A));
  }
  vector<int> d(N);
  for (int i = 0; i < N; i++){
    d[i] = E[i].size();
  }
  vector<string> ans(N);
  queue<int> Q;
  for (int i = 0; i < N; i++){
    if (d[i] == 1){
      Q.push(i);
    }
  }
  vector<bool> used(N, false);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    used[v] = true;
    for (auto e : E[v]){
      int w = e.second;
      if (d[w] > 1){
        if (v < w){
          ans[e.first] = "->";
        } else {
          ans[e.first] = "<-";
        }
        d[w]--;
        if (d[w] == 1){
          Q.push(w);
        }
      }
    }
  }
  for (int i = 0; i < N; i++){
    if (!used[i]){
      int v = i;
      used[v] = true;
      while (true){
        bool ok = false;
        for (auto e : E[v]){
          int w = e.second;
          if (!used[w]){
            if (v < w){
              ans[e.first] = "->";
            } else {
              ans[e.first] = "<-";
            }
            used[w] = true;
            v = w;
            ok = true;
            break;
          }
        }
        if (!ok){
          for (auto e : E[v]){
            int w = e.second;
            if (w == i){
              if (v < w){
                ans[e.first] = "->";
              } else {
                ans[e.first] = "<-";
              }
              break;
            }
          }
          break;
        }
      }
    }
  }
  for (int i = 0; i < N; i++){
    cout << ans[i] << endl;
  }
}
0