1811F - Is It Flower - CodeForces Solution


dfs and similar graphs implementation *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
vector <int> Con[200001];
int odwiedzone[200001];
void DFS(int a){
    odwiedzone[a] = 1;
    for (int i = 0; i < Con[a].size();i++){
        if(odwiedzone[Con[a][i]] == 0){
            DFS(Con[a][i]);
        }
    }
}
int DFS2(int a,int ojciec,int przodek){
    for (int i = 0; i < Con[a].size();i++){
        if(Con[a][i] != ojciec){
            if(Con[Con[a][i]].size() == 4){
                if(Con[a][i] != przodek)return 10000000;
                return 2;
            }
            else{
                return DFS2(Con[a][i],a,przodek) + 1;
            }
        }
    }
}

void solve(){
    int n,m;
    cin >> n >> m;
    for (int i = 1;i <= n;i++){
        Con[i].clear();
        odwiedzone[i] = 0;
    }
    for (int i = 1; i <= m;i++){
        int temp1,temp2;
        cin >> temp1 >> temp2;
        Con[temp1].push_back(temp2);
        Con[temp2].push_back(temp1);
    }
    long long licznik4 = 0;
    long long licznik2 = 0;
    bool is_flower = 1;
    for (int i = 1; i <= n;i++){
        if(Con[i].size() == 4){
            licznik4++;
        }
        if(Con[i].size() == 2){
            licznik2++;
        }
        if(Con[i].size() != 2 &&Con[i].size() != 4){
            is_flower = 0;
        }
    }
    //czy jest dobrze krawedzi
    if(licznik4 * licznik4 != licznik4 + licznik2){
        is_flower = 0;
    }
    //czy jest polaczony
    DFS(1);
    for (int i = 1;i <= n;i++){
        if(odwiedzone[i] == 0)is_flower = 0;
    }
    if(is_flower == 1){
        for (int i = 1; i <= n;i++){
            if(Con[i].size() == 4){
                int j = 0;
                while (j < Con[i].size() && Con[Con[i][j]].size() != 2){
                    j++;
                }
                if(j == Con[i].size())is_flower = 0;
                if( DFS2(Con[i][j],i,i) != licznik4)is_flower = 0;
            }
        }
        if(is_flower == 1){
            cout << "YES";
        }
        else{
            cout << "NO";
        }

    }
    else{
        cout << "NO";
    }

}
int main(){
    int q;
    cin >> q;
    while (q > 0){
        q--;
        solve();
        cout << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element