Submission #23239818


Source Code Expand

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
 
struct Edge {
  int to;
  long long w;
  Edge(int to, long long w) : to(to), w(w) {}
};
 
using Graph = vector<vector<int> >;
// using Graph = vector<vector<Edge> >;
 
template<class T> void chmax(T& a, T b) {
  if(a < b) a = b;
}
template<class T> void chmin(T& a, T b) {
  if(a > b) a = b;
}

 
struct UnionFind {
  vector<long long> par, siz;
 
  UnionFind(long long n) : par(n, -1), siz(n, 1){}
 
  long long root(long long x) {
    if(par[x] == -1) return x;
    else return par[x] = root(par[x]);
  }
 
  bool issame(long long x, long long y) {
    return root(x) == root(y);
  }
 
  bool unite(long long x, long long y) {
    x = root(x); y = root(y);
 
    if(x == y) return false;
 
    if(siz[x] < siz[y]) swap(x, y);
 
    par[y] = x;
    siz[x] += siz[y];
    return true;
  }
 
  long long size(long long x) {
    return siz[root(x)];
  }
};


int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<bool> > move(N, vector<bool>(N, false));

  
  Graph G(N);

  rep(i, M) {
    int s, e;
    cin >> s >> e;
    G[s-1].push_back(e-1);
  }

  stack<int> p;

  rep(s, N) {
    p.push(s);
    while(!p.empty()) {
      int n = p.top();
      p.pop();

      // nから1stepで行けるところ
      for(auto next : G[n]) {
        if(move[s][next]) continue;
        p.push(next);
        move[s][next] = true;
      }  
    }
  }



  // N`2
  int ans = 0;
  rep(i, N) {
    rep(j, N) {
      if(i == j) {
        ans++;
        continue;
      }
      if(move[i][j]) ans++;
    }
  }

  cout << ans << endl;
}

Submission Info

Submission Time
Task C - Tour
User Tommy3
Language C++ (GCC 9.2.1)
Score 300
Code Size 1831 Byte
Status AC
Exec Time 54 ms
Memory 4296 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 32
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, 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, random_20.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 13 ms 4216 KiB
hand_02.txt AC 2 ms 3432 KiB
random_01.txt AC 14 ms 4232 KiB
random_02.txt AC 26 ms 3684 KiB
random_03.txt AC 12 ms 4296 KiB
random_04.txt AC 11 ms 3900 KiB
random_05.txt AC 12 ms 4244 KiB
random_06.txt AC 24 ms 3712 KiB
random_07.txt AC 13 ms 4108 KiB
random_08.txt AC 7 ms 3664 KiB
random_09.txt AC 18 ms 4196 KiB
random_10.txt AC 22 ms 3584 KiB
random_11.txt AC 12 ms 4228 KiB
random_12.txt AC 10 ms 3852 KiB
random_13.txt AC 20 ms 4128 KiB
random_14.txt AC 11 ms 3572 KiB
random_15.txt AC 13 ms 4100 KiB
random_16.txt AC 3 ms 3440 KiB
random_17.txt AC 12 ms 4236 KiB
random_18.txt AC 18 ms 3932 KiB
random_19.txt AC 14 ms 4252 KiB
random_20.txt AC 2 ms 3532 KiB
random_31.txt AC 4 ms 3572 KiB
random_32.txt AC 8 ms 3568 KiB
random_33.txt AC 14 ms 4124 KiB
random_34.txt AC 54 ms 4272 KiB
random_35.txt AC 37 ms 4116 KiB
random_36.txt AC 12 ms 4256 KiB
random_37.txt AC 13 ms 4068 KiB
sample_01.txt AC 2 ms 3572 KiB
sample_02.txt AC 2 ms 3568 KiB
sample_03.txt AC 2 ms 3544 KiB