Submission #49633059


Source Code Expand

#include <bits/stdc++.h>
#include <atcoder/modint>
const int MOD=998244353;
using mint = atcoder::static_modint<MOD>;
using namespace std;
typedef long long ll;
#define rep(i,a,n) for (ll i=a;i<(ll)(n);i++)
ll read(){ll r;scanf("%lld",&r);return r;}
vector<pair<int,int> > g[200010]; // u->{v,eid}
vector<pair<int,int> > g2[200010]; // groupu -> {groupv,old eid}

int dep[200010];
int mindep[200010];
array<int,2> edge[200010];
bool vis[200010];
int key[200010];
void dfs(int u,int fa,int d){
  vis[u]=true;
  dep[u] = d;
  mindep[u]=d;
  for(auto &[v,eid]:g[u]) if(v!=fa){
    if(!vis[v]) dfs(v,u,d+1);
    if(mindep[v] > dep[u]) {
      // printf("keypath : %d -> %d\n",u,v);
      key[eid] = true;
    }
    mindep[u] = min(mindep[u],mindep[v]);
  }
  // printf("dep[%d]=%d, mindep[%d]=%d\n",u,dep[u],u,mindep[u]);
}
int uf[200010];
int getfa(int u){ return uf[u]==u?u:uf[u]=getfa(uf[u]); }
void dfs2(int u, int fa){
  vis[u] = true;
  for(auto &[v,eid]:g[u]) if(v!=fa){
    if(!vis[v]) dfs2(v,u);
    if(!key[eid]) {
      // printf("link %d <-> %d\n",u,v);
      uf[getfa(u)] = getfa(v);
  }
  }
}

bool dfs3(int u,int fa,int dst, vector<int>&path){
  assert(u==getfa(u));
  if(u==dst) {
    return true;
  }
  vis[u]=true;
  for(auto [v,eid]:g2[u]) if(getfa(v) != fa and !vis[v]) {
    assert(key[eid]);
    bool ret = dfs3(getfa(v),u,dst,path);
    if(ret) {// u->v能找到dst
      path.push_back(eid);
      return ret;
    }
  }
  return false;

}

int main(){
  int n=read();
  int m=read();
  int a=read()-1;
  int b=read()-1;
  int c=read()-1;
  int eid = 0;
  rep(i,0,m) {
    int u=read()-1;
    int v=read()-1;
    g[u].push_back({v,eid});
    g[v].push_back({u,eid});
    edge[eid]={u,v};
    eid++;
  }
  // 找关键路径
  dfs(0,0,0);
  // 合并非关键路径上的点
  iota(uf,uf+n,0);
  fill(vis,vis+n,false);
  dfs2(0,0);

  rep(u,0,n) {
    int fu=getfa(u);
    for(auto [v,eid]:g[u]) {
      int fv = getfa(v);
      if(fv != fu) g2[fu].push_back({fv,eid}); // 可能重边
    }
  }
  // rep(u,0,n) if(u==getfa(u)){
  //   sort(begin(g2[u]),end(g2[u]));
  // }

  // 新的树上做简单路径查找
  fill(vis,vis+n,false);
  vector<int> path; // path是所有eid list, 从c到a的 eid list, 因为是递归push的所以方向相反
  dfs3(getfa(a),getfa(a),getfa(c),path); // 一定全是key 路径

  vector<int> vertexs = {c};
  for(auto eid:path){
    auto [u,v] = edge[eid];
    if(getfa(u) == getfa(vertexs.back())){
      vertexs.push_back(u); // -u-v-
      vertexs.push_back(v); // -u-v-
    }else{
      assert(getfa(v) == getfa(vertexs.back()));
      vertexs.push_back(v); // -u-v-
      vertexs.push_back(u); // -u-v-
    }
  }
  vertexs.push_back(a);
  // for(auto vertex:vertexs) printf("%d -> ",vertex);
  for(int i=0;i<(int)size(vertexs);i+=2) if(getfa(b) == getfa(vertexs[i])) {
    if(vertexs[i] != vertexs[i+1]) { // ===?-b-?===
      printf("Yes\n");
      return 0;
    }else{
      if(vertexs[i] == b) { // ===b===
        printf("Yes\n");
        return 0;
      }
    }
  }
  printf("No\n");
  return 0;
}

// 简单无向连通图N点,m边
// n 2e5
// m 2e5
// 判断是否有简单路径连接 a,b,c三个点(保证不同)
// 如果是树,也就是u..v的简单路径上 或者u外侧,v外侧
// 或者是选一个点,其它点在分开的3个子树里
// 如果两点在一个环上, 那么第三点 到最近的环的点连线再绕一圈一定是方案
// 否则a,b,c任意两点不在任何环上
// 说明 a...b 的路径中有至少一个关键边????
// 如果a..b存在两条无重复的路径,则在环上,
// 否则a..b的所有路径两两有重复边
// 如果没有关键路径,那么 去掉每条边依然连通,可以构造出一条新路径
// 所以找 关键路径
// 对非关键路径 并查集缩点,最后就是树上判断
// dfs(u,fa)
//  minvis[u] = u;
//  for v in e[u] and v != fa:
//    minvis[u] = min(dfs(v,u))
// 关键路径满足dfs能访问的点的最小深度 = u?

Submission Info

Submission Time
Task G - Typical Path Problem
User cromarmot
Language C++ 20 (gcc 12.2)
Score 0
Code Size 3983 Byte
Status WA
Exec Time 278 ms
Memory 51668 KiB

Compile Error

Main.cpp: In function ‘ll read()’:
Main.cpp:8:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    8 | ll read(){ll r;scanf("%lld",&r);return r;}
      |                ~~~~~^~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 575
Status
AC × 3
AC × 77
WA × 16
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand2_00.txt, hand2_01.txt, hand2_02.txt, hand2_03.txt, hand2_04.txt, hand2_05.txt, hand2_06.txt, hand2_07.txt, hand2_08.txt, hand2_09.txt, hand2_10.txt, hand2_11.txt, hand2_12.txt, hand2_13.txt, hand2_14.txt, hand2_15.txt, hand2_16.txt, hand2_17.txt, hand2_18.txt, hand2_19.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random2_00.txt, random2_01.txt, random2_02.txt, random2_03.txt, random2_04.txt, random2_05.txt, random2_06.txt, random2_07.txt, random2_08.txt, random2_09.txt, random2_10.txt, random2_11.txt, random2_12.txt, random2_13.txt, random2_14.txt, random2_15.txt, random2_16.txt, random2_17.txt, random2_18.txt, random2_19.txt, random2_20.txt, random2_21.txt, random2_22.txt, random2_23.txt, random2_24.txt, random3_00.txt, random3_01.txt, random3_02.txt, random3_03.txt, random3_04.txt, random3_05.txt, random3_06.txt, random3_07.txt, random3_08.txt, random3_09.txt, random3_10.txt, random3_11.txt, random3_12.txt, random3_13.txt, random3_14.txt, random3_15.txt, random3_16.txt, random3_17.txt, random3_18.txt, random3_19.txt, random_00.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
Case Name Status Exec Time Memory
example_00.txt AC 3 ms 3612 KiB
example_01.txt AC 3 ms 3740 KiB
example_02.txt AC 3 ms 3744 KiB
hand2_00.txt WA 118 ms 18860 KiB
hand2_01.txt WA 135 ms 19164 KiB
hand2_02.txt WA 114 ms 17532 KiB
hand2_03.txt AC 134 ms 20932 KiB
hand2_04.txt AC 127 ms 19592 KiB
hand2_05.txt WA 119 ms 17768 KiB
hand2_06.txt WA 112 ms 17624 KiB
hand2_07.txt WA 133 ms 20936 KiB
hand2_08.txt WA 132 ms 17888 KiB
hand2_09.txt WA 132 ms 18948 KiB
hand2_10.txt AC 70 ms 13292 KiB
hand2_11.txt AC 149 ms 22624 KiB
hand2_12.txt WA 18 ms 6756 KiB
hand2_13.txt WA 21 ms 6640 KiB
hand2_14.txt WA 28 ms 7680 KiB
hand2_15.txt WA 25 ms 7472 KiB
hand2_16.txt WA 75 ms 15560 KiB
hand2_17.txt WA 88 ms 16192 KiB
hand2_18.txt AC 143 ms 30920 KiB
hand2_19.txt WA 124 ms 23304 KiB
hand_00.txt AC 3 ms 3736 KiB
hand_01.txt AC 226 ms 43756 KiB
hand_02.txt AC 190 ms 34556 KiB
hand_03.txt AC 33 ms 10332 KiB
hand_04.txt AC 34 ms 10320 KiB
hand_05.txt AC 274 ms 51644 KiB
hand_06.txt AC 271 ms 51664 KiB
hand_07.txt AC 186 ms 34392 KiB
hand_08.txt AC 278 ms 49196 KiB
hand_09.txt AC 275 ms 51668 KiB
random2_00.txt AC 8 ms 4720 KiB
random2_01.txt AC 21 ms 7064 KiB
random2_02.txt AC 11 ms 5460 KiB
random2_03.txt AC 26 ms 8264 KiB
random2_04.txt AC 21 ms 6848 KiB
random2_05.txt AC 15 ms 6080 KiB
random2_06.txt AC 42 ms 10572 KiB
random2_07.txt AC 24 ms 7696 KiB
random2_08.txt AC 48 ms 11124 KiB
random2_09.txt AC 31 ms 9196 KiB
random2_10.txt AC 30 ms 10472 KiB
random2_11.txt AC 31 ms 8868 KiB
random2_12.txt AC 42 ms 9960 KiB
random2_13.txt AC 38 ms 9596 KiB
random2_14.txt AC 32 ms 8820 KiB
random2_15.txt AC 74 ms 18136 KiB
random2_16.txt AC 57 ms 16164 KiB
random2_17.txt AC 74 ms 16892 KiB
random2_18.txt AC 64 ms 16520 KiB
random2_19.txt AC 50 ms 15080 KiB
random2_20.txt AC 144 ms 24220 KiB
random2_21.txt AC 109 ms 20880 KiB
random2_22.txt AC 173 ms 31284 KiB
random2_23.txt AC 163 ms 26960 KiB
random2_24.txt AC 132 ms 24304 KiB
random3_00.txt AC 172 ms 22180 KiB
random3_01.txt AC 148 ms 20648 KiB
random3_02.txt AC 159 ms 23708 KiB
random3_03.txt AC 92 ms 15572 KiB
random3_04.txt AC 134 ms 20952 KiB
random3_05.txt AC 136 ms 20544 KiB
random3_06.txt AC 73 ms 15204 KiB
random3_07.txt AC 160 ms 22880 KiB
random3_08.txt AC 111 ms 19196 KiB
random3_09.txt AC 109 ms 18532 KiB
random3_10.txt AC 132 ms 18504 KiB
random3_11.txt AC 136 ms 21540 KiB
random3_12.txt AC 69 ms 15804 KiB
random3_13.txt AC 137 ms 22780 KiB
random3_14.txt AC 149 ms 22196 KiB
random3_15.txt AC 82 ms 17288 KiB
random3_16.txt AC 89 ms 16472 KiB
random3_17.txt AC 69 ms 13208 KiB
random3_18.txt WA 97 ms 16312 KiB
random3_19.txt AC 139 ms 20396 KiB
random_00.txt AC 3 ms 3904 KiB
random_01.txt AC 3 ms 4012 KiB
random_02.txt AC 4 ms 3988 KiB
random_03.txt AC 4 ms 3840 KiB
random_04.txt AC 3 ms 3992 KiB
random_05.txt AC 3 ms 4012 KiB
random_06.txt AC 3 ms 3964 KiB
random_07.txt AC 3 ms 3996 KiB
random_08.txt AC 4 ms 3888 KiB
random_09.txt AC 3 ms 3968 KiB
random_10.txt AC 3 ms 4032 KiB
random_11.txt AC 3 ms 4076 KiB
random_12.txt AC 3 ms 3968 KiB
random_13.txt AC 3 ms 4016 KiB
random_14.txt AC 4 ms 4104 KiB