답안 #741148

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
741148 2023-05-13T16:07:22 Z Dan4Life Jail (JOI22_jail) C++17
0 / 100
56 ms 5076 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
const int mxN = (int)1.2e5+10;
const int mxM = (int)510;
int n, m, Cycle;
int ind[mxN], dis[mxM][mxN];
int s[mxM], t[mxM], vis[mxM];
vector<int> adj[mxN], adj2[mxM];

void dfs(int s, int p, int i){
	if(p==-1) dis[i][s] = 0;
	else dis[i][s] = dis[i][p]+1;
	for(auto u : adj[s])
		if(u!=p) dfs(u,s,i);
}

bool inPath(int x, int s, int t){
	return (dis[ind[s]][x]+dis[ind[t]][x]==dis[ind[s]][t]);
}

void dfs2(int s){
	vis[s]=1;
	for(auto u : adj2[s]){
		if(!vis[u]) dfs2(u);
		else if(vis[u]==1) { Cycle=1; return; }
	}
	vis[s]=2;
}

void solve(){
	cin >> n; Cycle = 0;
	for(int i = 1; i <= n; i++) adj[i].clear();
	for(int i = 0; i < m; i++) adj2[i].clear();
	for(int i = 0; i < n-1; i++){
		int a, b; cin >> a >> b;
		adj[a].pb(b), adj[b].pb(a);
	}
	for(int i = 1; i <= n; i++) dfs(i,-1,i);
	for(int i = 0; i < m; i++) vis[i]=0;
	cin >> m;
	vector<int> v; v.clear();
	for(int i = 0; i < m; i++) 
		cin >> s[i] >> t[i], v.pb(s[i]), v.pb(t[i]);
	sort(all(v)); v.erase(unique(all(v)),end(v));
	for(int i = 0; auto u : v) ind[u]=i, dfs(u,-1,i), i++;
	for(int i = 0; i < m; i++)
		for(int j = 0; j < m; j++)
			if(i!=j and inPath(s[j],s[i],t[i])) adj2[j].pb(i);
	for(int i = 0; i < m; i++) if(!vis[i]) dfs2(i);
	cout << (!Cycle?"Yes":"No") << "\n";
}

int32_t main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	int t = 1; cin >> t; while(t--) solve();
}

Compilation message

jail.cpp: In function 'void solve()':
jail.cpp:50:17: warning: range-based 'for' loops with initializer only available with '-std=c++2a' or '-std=gnu++2a'
   50 |  for(int i = 0; auto u : v) ind[u]=i, dfs(u,-1,i), i++;
      |                 ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Correct 2 ms 3156 KB Output is correct
4 Incorrect 56 ms 3924 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Incorrect 11 ms 5076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Incorrect 11 ms 5076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Incorrect 11 ms 5076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Incorrect 11 ms 5076 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Correct 2 ms 3168 KB Output is correct
4 Correct 2 ms 3156 KB Output is correct
5 Incorrect 10 ms 3300 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Correct 2 ms 3156 KB Output is correct
4 Incorrect 56 ms 3924 KB Output isn't correct
5 Halted 0 ms 0 KB -