답안 #741146

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
741146 2023-05-13T16:03:35 Z Dan4Life Jail (JOI22_jail) C++17
0 / 100
61 ms 7892 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;
const int mxM = (int)500;
const int LINF = (int)2e18;
int n, m, Cycle;
int ind[mxN], dis[mxM][mxN];
int s[mxM], t[mxM], vis[mxM];
vector<int> adj[mxN], adj2[mxN];

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++);
	for(int i = 0; i < m; i++){
		for(int j = 0; j < m; j++){
			if(i==j) continue;
			int a = s[i], b = t[i], c = s[j], d = t[j];
			if(inPath(c,a,b)) 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:51:17: warning: range-based 'for' loops with initializer only available with '-std=c++2a' or '-std=gnu++2a'
   51 |  for(int i = 0; auto u : v) ind[u]=i, dfs(u,-1,i++);
      |                 ^~~~
jail.cpp:55:38: warning: unused variable 'd' [-Wunused-variable]
   55 |    int a = s[i], b = t[i], c = s[j], d = t[j];
      |                                      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 3 ms 5964 KB Output is correct
4 Incorrect 61 ms 7044 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Incorrect 13 ms 7892 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Incorrect 13 ms 7892 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Incorrect 13 ms 7892 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Incorrect 13 ms 7892 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 4 ms 5960 KB Output is correct
4 Correct 4 ms 5968 KB Output is correct
5 Incorrect 11 ms 6228 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5972 KB Output is correct
2 Correct 3 ms 5972 KB Output is correct
3 Correct 3 ms 5964 KB Output is correct
4 Incorrect 61 ms 7044 KB Output isn't correct
5 Halted 0 ms 0 KB -