답안 #42219

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
42219 2018-02-23T18:00:14 Z octopuses Pipes (CEOI15_pipes) C++14
100 / 100
1252 ms 8312 KB
//Giorgi Kldiashvili

#include <bits/stdc++.h>

#define ll long long
#define M 1000000007ll

using namespace std;
const int N = 100001;

int n, m, x, y, timer;

int p[N], P[N], c[N], d[N], in[N], out[N];
vector < int > G[N];
bool used[N];

int Parent(int x)
{
  if(P[x] == x)
    return x;
  return P[x] = Parent(P[x]);
}

void go(int v)
{
  c[v] = d[v] = in[v] = ++ timer;
  used[v] = true;
  for(int i = 0; i < G[v].size(); ++ i)
  {
    if(used[G[v][i]])
      continue;
    p[G[v][i]] = v;
    go(G[v][i]);
  }
  out[v] = timer;
}
void dfs(int v)
{
  used[v] = true;
  for(int i = 0; i < G[v].size(); ++ i)
  {
    if(used[G[v][i]])
      continue;
    dfs(G[v][i]);
    c[v] = min(c[v], c[G[v][i]]);
    d[v] = max(d[v], d[G[v][i]]);
  }
  if(p[v] == 0)
    return;
  if(c[v] >= in[v] && d[v] <= out[v])
    printf("%d %d\n", v, p[v]);
}

int main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);

  scanf("%d %d", &n, &m);

  for(int i = 1; i <= n; ++ i)
    P[i] = i;

  while(m --)
  {
    char ch = getchar();
    x = y = 0;
    while(ch > '9' || ch < '0')
      ch = getchar();
    while(ch >= '0' && ch <= '9')
      x = x * 10 + (ch - '0'), ch = getchar();
    ch = getchar();
    while(ch > '9' || ch < '0')
      ch = getchar();
    while(ch >= '0' && ch <= '9')
      y = y * 10 + (ch - '0'), ch = getchar();
    if(Parent(x) == Parent(y))
      continue;
    P[Parent(x)] = y;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  for(int i = 1; i <= n; ++ i)
  {
    if(used[i]) continue;
    go(i);
  }
  rewind(stdin);

  scanf("%d %d", &n, &m);
  for(int i = 1; i <= n; ++ i)
    P[i] = i, used[i] = 0;
  while(m --)
  {
    char ch = getchar();
    x = y = 0;
    while(ch > '9' || ch < '0')
      ch = getchar();
    while(ch >= '0' && ch <= '9')
      x = x * 10 + (ch - '0'), ch = getchar();
    ch = getchar();
    while(ch > '9' || ch < '0')
      ch = getchar();
    while(ch >= '0' && ch <= '9')
      y = y * 10 + (ch - '0'), ch = getchar();
    if(Parent(x) == Parent(y))
    {
      c[x] = min(c[x], in[y]);
      d[x] = max(d[x], in[y]);
      d[y] = max(d[y], in[x]);
      c[y] = min(c[y], in[x]);
      continue;
    }
    P[Parent(x)] = y;
  }
  for(int i = 1; i <= n; ++ i)
  {
    if(used[i]) continue;
    dfs(i);
  }
}

Compilation message

pipes.cpp: In function 'void go(int)':
pipes.cpp:28:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < G[v].size(); ++ i)
                  ~~^~~~~~~~~~~~~
pipes.cpp: In function 'void dfs(int)':
pipes.cpp:40:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < G[v].size(); ++ i)
                  ~~^~~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &m);
   ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &m);
   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2944 KB Output is correct
2 Correct 7 ms 2944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 2816 KB Output is correct
2 Correct 93 ms 3036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 3224 KB Output is correct
2 Correct 186 ms 3192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 293 ms 3960 KB Output is correct
2 Correct 260 ms 4472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 406 ms 6612 KB Output is correct
2 Correct 367 ms 6764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 630 ms 7108 KB Output is correct
2 Correct 638 ms 7280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 858 ms 8152 KB Output is correct
2 Correct 817 ms 8308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1051 ms 8164 KB Output is correct
2 Correct 1026 ms 8292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1252 ms 7980 KB Output is correct
2 Correct 1240 ms 8312 KB Output is correct