Submission #68195328


Source Code Expand

#include<bits/stdc++.h>
using namespace std; 
signed main() {
  int t; 
  cin>>t;
  
  function<int(vector<int>&,int)> gp = [&](vector<int> & p, int u) {
    return p[u] == u? u: p[u] = gp(p, p[u]); 
  };
  
  auto merge = [&](vector<int>& p, int u, int v) {
    auto p1 = gp(p,u), p2 = gp(p,v);
    p[p1] = p2; 
  };
  
  auto same = [&](vector<int> & p,int u, int v) -> bool {
    return gp(p,u) == gp(p,v); 
  };
  while(t--) {
    int n; 
    cin>>n; 
    int m; 
    cin>>m;
    int x,y; 
    cin>>x>>y; 
    x--; y--;
    vector<vector<int>> g(n); 
    for(int i=0;i<m;i++) {
      int u,v; 
      cin>>u>>v; 
      u--; v--; 
      g[u].push_back(v);
      g[v].push_back(u); 
    }
    for(int i=0;i<n;i++) sort(g[i].begin(), g[i].end()); 
    vector<bool> vis(n, false);
    vector<int> p(n, -1);  
    function<bool(int)> dfs = [&](int u) {
      if (u == y){
        vis[u] = true; 
        return true; 
      }
      vector<int> par(n);
      iota(par.begin(), par.end(),0); 
      for(int i=0;i<n;i++) {
        if(vis[i])continue; 
        for(auto v:g[i]) {
          if (vis[v])continue; 
          merge(par, i,v); 
        }
      }
      if (!same(par, u, y)) return false; 
      vis[u] = true; 
      for(auto v:g[u]) {
        if (vis[v])continue;
        p[v] = u; 
        if (!dfs(v)) {
          p[v] = -1; 
          continue; 
        }
        return true; 
      }
      return false;
    };
    dfs(x); 
    int cur = y;
    deque<int> ans; 
    while(cur!=-1) {
      ans.push_front(cur); 
      cur = p[cur]; 
    }
    for(auto i: ans) { cout<< i + 1 << " "; }
    cout<<endl; 
  }
}

Submission Info

Submission Time
Task E - A Path in A Dictionary
User mafailure
Language C++ 17 (Clang 16.0.6)
Score 475
Code Size 1692 Byte
Status AC
Exec Time 795 ms
Memory 7780 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 1
AC × 43
Set Name Test Cases
Sample example_00.txt
All example_00.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, hand_10.txt, hand_11.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, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3576 KiB
hand_00.txt AC 124 ms 4412 KiB
hand_01.txt AC 17 ms 7316 KiB
hand_02.txt AC 303 ms 4272 KiB
hand_03.txt AC 17 ms 7304 KiB
hand_04.txt AC 41 ms 3632 KiB
hand_05.txt AC 130 ms 5512 KiB
hand_06.txt AC 795 ms 6884 KiB
hand_07.txt AC 5 ms 3500 KiB
hand_08.txt AC 323 ms 5892 KiB
hand_09.txt AC 410 ms 7780 KiB
hand_10.txt AC 25 ms 7444 KiB
hand_11.txt AC 2 ms 3472 KiB
random_00.txt AC 2 ms 3572 KiB
random_01.txt AC 2 ms 3528 KiB
random_02.txt AC 2 ms 3484 KiB
random_03.txt AC 2 ms 3532 KiB
random_04.txt AC 3 ms 3588 KiB
random_05.txt AC 4 ms 3504 KiB
random_06.txt AC 2 ms 3404 KiB
random_07.txt AC 2 ms 3508 KiB
random_08.txt AC 4 ms 3612 KiB
random_09.txt AC 9 ms 3492 KiB
random_10.txt AC 12 ms 3604 KiB
random_11.txt AC 13 ms 3524 KiB
random_12.txt AC 2 ms 3672 KiB
random_13.txt AC 2 ms 3488 KiB
random_14.txt AC 7 ms 3588 KiB
random_15.txt AC 25 ms 3544 KiB
random_16.txt AC 31 ms 3572 KiB
random_17.txt AC 46 ms 3804 KiB
random_18.txt AC 2 ms 3508 KiB
random_19.txt AC 1 ms 3584 KiB
random_20.txt AC 348 ms 6636 KiB
random_21.txt AC 8 ms 3624 KiB
random_22.txt AC 119 ms 4168 KiB
random_23.txt AC 261 ms 4508 KiB
random_24.txt AC 2 ms 3676 KiB
random_25.txt AC 11 ms 3852 KiB
random_26.txt AC 427 ms 7440 KiB
random_27.txt AC 220 ms 5080 KiB
random_28.txt AC 112 ms 4624 KiB
random_29.txt AC 137 ms 4780 KiB