fork download
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3. typedef long long int ll;
  4. const ll INF = 1e9 + 7;
  5. #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  6.  
  7. void gg() {
  8. #ifndef ONLINE_JUDGE
  9. freopen("input.txt", "r", stdin);
  10. freopen("output.txt", "w", stdout);
  11. #endif
  12. }
  13.  
  14. int main() {
  15.  
  16. // gg();
  17. ll tt; cin>>tt;
  18. while(tt--) {
  19. ll n, r, m; cin>>n>>r>>m;
  20. vector<ll> vect[n+2];
  21. set<ll> st;
  22. for(ll i=0;i<r;i++) {
  23. ll a, b; cin>>a>>b;
  24. vect[a].push_back(b);
  25. vect[b].push_back(a);
  26. st.insert(a);
  27. st.insert(b);
  28. }
  29. map<ll, ll> mp;
  30. for(ll i=0;i<m;i++) {
  31. ll a, b; cin>>a>>b;
  32. mp[a] = b;
  33. }
  34. vector<bool> vis(n+2, false);
  35. bool flag = true;
  36. for(auto it:mp) {
  37. ll city = it.first;
  38. ll len = it.second;
  39. queue<pair<ll, ll > > q;
  40. set<ll> st1;
  41. q.push({it.first, it.second});
  42. while(!q.empty()) {
  43. city = q.front().first;
  44. len = q.front().second;
  45. q.pop();
  46. if(vis[city]) {
  47. flag = false;
  48. goto end;
  49. }
  50. vis[city] = true;
  51. st1.insert(city);
  52. if(len > 0) {
  53. for(auto it:vect[city]) {
  54. if(st1.find(it) != st1.end()) continue;
  55. q.push({it, len-1});
  56. }
  57. }
  58. }
  59. }
  60. end:;
  61. for(auto it:st) {
  62. if(!vis[it]) {
  63. flag = false; break;
  64. }
  65. }
  66. if(flag) cout<<"Yes\n";
  67. else cout<<"No\n";
  68. }
  69. return 0;
  70. }
Success #stdin #stdout 0s 4892KB
stdin
2
3 2 2
1 2
2 3
1 2
2 0
4 5 2
1 4
1 2
1 3
4 2
3 4
2 1
3 0
stdout
No
Yes