Skip to content

Instantly share code, notes, and snippets.

@karngyan
Created May 23, 2019 14:26
Show Gist options
  • Save karngyan/53b2f7f8bc165e609cc65a8b4088c426 to your computer and use it in GitHub Desktop.
Save karngyan/53b2f7f8bc165e609cc65a8b4088c426 to your computer and use it in GitHub Desktop.
/*
Author : @karngyan
*/
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = std::vector< ll >;
const ll N = 1e6+2;
vll g[N];
vll god;
ll n , r, m;
bool bfs(ll source_node , ll strength){
ll level = 1;
if(god[source_node] != -1){
return false;
}
vll all;
all.push_back(source_node);
god[source_node] = source_node;
while(level <= strength){
vll new_all;
for(auto it : all){
for(auto y : g[it]){
if(god[y] == -1){
god[y] = source_node;
new_all.push_back(y);
}
else if(god[y] == source_node)
continue;
else
return false;
}
}
all = new_all;
level++;
}
return true;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
ll t;
cin >> t;
while(t--)
{
cin >> n >> r >> m;
god.clear();
god.resize(n+1 , -1);
for(int i = 0 ; i<= n ; ++i){
g[i].clear();
}
for(int i = 0 ; i < r ; ++i){
ll u , v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
string ans = "Yes";
for(int i = 0 ; i<m ; ++i){
ll k , s;
cin >> k >> s;
bool check = bfs(k , s);
if(!check){
ans = "No";
break;
}
}
// given in question that each node should have exactly one god
for(ll i =1 ; i<=n ; ++i){
if(god[i] == -1){
ans = "No";
break;
}
}
cout << ans << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment