Birthday Party – Kattis, Kattis: Birthday Party – Kattis, Kattis
code:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define inf LONG_LONG_MAX
#define vi vector<int>
#define all(v) (v.begin(),v.end())
#define loop(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define out(x) for(auto it : x){ cout<<it<<' ';} cout<<endl;
#define yes cout<<"Yes"<<'\n'
#define no cout<<"No"<<'\n'
void setIO(string name = "") { // name is nonempty for USACO file I/O
ios_base::sync_with_stdio(0);
cin.tie(0); // see Fast Input & Output
if (name.size()) {
freopen((name + ".in").c_str(), "r", stdin); // see Input & Output
freopen((name + ".out").c_str(), "w", stdout);
}
}
int M=1e9+7;
vector<vi>g;
vi vis;
void dfs(int ver){
if(vis[ver]==1)return;
vis[ver]=1;
for(auto it:g[ver]){
dfs(it);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
setIO();
int x,y;
cin>>x>>y;
while (x!=0 && y!=0)
{
g.clear();
vis.clear();
g.resize(x);
vis.resize(x,0);
loop(y){
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
// needs to have a connected component each having two edges
// nice hack if connected component chk is missing
int cnt=0;
dfs(0);
loop(x){
if(g[i].size()<2){cnt++;}
if(vis[i]==0){cnt++;}
}
out(vis);
(cnt!=0)?yes:no;
cin>>x>>y;
}
return 0;
}
Issue: got wrong ans, is checking graph is connected and each having at least two edges is sufficient for deciding yes, no.