From 3dc48180cf8ec5d85cab9ca23984cfe385ff143b Mon Sep 17 00:00:00 2001 From: ashutosh Date: Wed, 10 Apr 2024 23:32:49 +0530 Subject: [PATCH] chore: solved day 15 hard task --- hard/day_15/solution.cpp | 79 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/hard/day_15/solution.cpp b/hard/day_15/solution.cpp index 7c77207..54deaf7 100644 --- a/hard/day_15/solution.cpp +++ b/hard/day_15/solution.cpp @@ -1 +1,78 @@ -//write you code here \ No newline at end of file +//write you code here +#include +using namespace std; +bool isBip(vector > &adj, int n){ + vector side(n, -1); + bool is_bipartite = true; + queue q; + for (int st = 0; st < n; ++st) { + if (side[st] == -1) { + q.push(st); + side[st] = 0; + while (!q.empty()) { + int v = q.front(); + q.pop(); + for (int i = 0; i < adj[v].size() ; i++) { + int u = adj[v][i]; + if (side[u] == -1) { + side[u] = side[v] ^ 1; + q.push(u); + } else { + is_bipartite &= side[u] != side[v]; + } + } + } + } + } + return is_bipartite; +} +int main(){ + int t; + cin>>t; + while(t--){ + int n,a,b; + cin>>n; + vector > in(n); + bool flag = true; + int cnt[n+1] = {0}; + int i = 0; + vector > edge; + edge.resize(n+1); + while(i>a>>b; + in[i].first = a; + in[i].second = b; + edge[a].push_back(i); + edge[b].push_back(i); + if(!flag){ + i++; + continue; + } + if( a == b) { + flag = false; + } + cnt[a]++; + cnt[b]++; + if(cnt[a] > 2 || cnt[b] > 2) { + flag = false; + } + i++; + } + if(!flag){ + cout<<"NO\n"; + continue; + } + vector > adj; + adj.resize(n); + for(i = 1 ; i <= n ; i ++ ){ + int u = edge[i][0]; + int v = edge[i][1]; + adj[u].push_back(v); + adj[v].push_back(u); + } + if(!isBip(adj,n)) cout<<"NO\n"; + else cout<<"YES\n"; + + } + return 0; +} \ No newline at end of file