Problem 863F : Is it Flower ? || Full Detailed Solution || Codeforces

 Question : 



Vlad found a flowerbed with graphs in his yard and decided to take one for himself. Later he found out that in addition to the usual graphs, -flowers also grew on that flowerbed. A graph is called a -flower if it consists of a simple cycle of length , through each vertex of which passes its own simple cycle of length  and these cycles do not intersect at the vertices. For example, 3-flower looks like this:

Note that 1-flower and 2-flower do not exist, since at least 3 vertices are needed to form a cycle.

Vlad really liked the structure of the -flowers and now he wants to find out if he was lucky to take one of them from the flowerbed.


Input

The first line of input contains the single integer  (1104) — the number of test cases in the test.

The descriptions of the cases follow. An empty string is written before each case.

The first line of each case contains two integers  and  (221051min(2105,(1)2)) — the number of vertices and edges in the graph, respectively.

The next  lines contain two integers each  and  (1,) — numbers of vertices connected by an edge. It is guaranteed that the graph does not contain multiple edges and self-loops.

It is guaranteed that the sum of  over all test cases does not exceed 2105. It is also guaranteed for the sum of  over all test cases.


Output

Output  lines, each of which is the answer to the corresponding test case. As an answer, output "YES" if Vlad's graph is a -flower for some , and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

Examples

input
Copy
5

9 12
1 2
3 1
2 3
1 6
4 1
6 4
3 8
3 5
5 8
9 7
2 9
7 2

8 12
1 2
3 1
2 3
1 6
4 1
6 4
3 8
3 5
5 8
8 7
2 8
7 2

4 3
1 2
4 2
3 1

6 8
6 3
6 4
5 3
5 2
3 2
3 1
2 1
2 4

5 7
2 4
2 5
3 4
3 5
4 1
4 5
1 5
output
Copy
YES
NO
NO
NO
NO
input
Copy
4

2 1
1 2

8 9
1 2
8 4
8 2
6 4
6 5
4 7
3 2
3 7
2 5

9 12
2 9
2 8
6 9
6 8
6 5
6 1
9 8
9 3
9 1
8 3
8 7
5 7

3 3
1 2
1 3
2 3
output
Copy
NO
NO
NO
NO

Explanation :

First from k-flower, number of nodes n=k*k and number of edges m=k*(k+1). Then there must be k nodes with degree 4, and other nodes with degree 2. If so, 4-nodes must form a cycle of size k (we can take all edges between 4-nodes and check if they're a cycle), and all other edges must form k cycles of size k, and each cycle contains one 4-node and k-1 2-nodes.

Solution :

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

vector<int> adj[200010];
vector<int> vis(200010);

int dfs(int root){
  int ans = 1 - vis[root];
  vis[root] = 1;
  for(int c : adj[root]){
    if(vis[c] == 0){
      ans += dfs(c);
      break;
    }
  }
  return ans;
}


void solve(){
  int n,m;
  int a,b;

  string s;
  getline(cin, s);
  getline(cin, s);
  cin>>n>>m;
  for(int i = 0;i<n;i++)adj[i].clear(),vis[i]=0;
  int sq1 = int(sqrt(n)),sq;
  
  for(int i = sq1-2;i<sq1+3;i++){
    if(i*i<=n){
      sq = i;
    }
  }
  vector<int> v(n,0);
  for(int i = 0;i<m;i++){
    cin>>a>>b;
    a--;b--;
    v[a]++;
    v[b]++;
    adj[a].push_back(b);
    adj[b].push_back(a);
    
  }

  if(sq*sq != n || sq < 3){
    cout<<"NO\n";
    return;
  }

  int pp=0,np=0;

  for(int i = 0;i<n;i++){
    if(v[i] == 2){
      np++;
    }else if(v[i] == 4){
      pp++;
      vis[i] = 1;
    }else{
      cout<<"NO\n";
      return;
    }
  }

  for(int i = 0;i<n;i++){
    if(v[i] == 4){
      int dd = dfs(i);
      if(dd != sq-1){
        cout<<"NO\n";
        return ;
      }
      
    }
  }
  int lst;
  for(int i = 0;i<n;i++){
    if(v[i] == 4){
      vis[i] = 0;
      lst = i;
    }
  }

  if(dfs(lst) != sq){
    cout<<"NO\n";
    return;
  }

  if(pp == sq && np + pp == n ){
    cout<<"YES\n";
  }else{
    cout<<"NO\n";
  }


}

int main(){
    ll t,n,x1,y1,x2,y2;
    cin>>t;
     while(t--){
       solve();
     }
   return 0;

}



I Hope you liked it please tell me the suggestion in the comment below what shoud I change according to you.
And discuss the Approach in comment section if you want. share the solution with you frnds too.
Previous Post Next Post