-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolorthegraph.cpp
More file actions
executable file
·92 lines (79 loc) · 1.21 KB
/
colorthegraph.cpp
File metadata and controls
executable file
·92 lines (79 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
#define ll long long int
#define ii pair<int, int>
#define pb push_back
#define f first
#define s second
using namespace std;
#define SIZE 1000001
vector<int> adj[SIZE];
bool vis[SIZE];
ll color[SIZE];
ll dfs(ll r)
{
ll ans=0, sz=0;
stack<ll>s;
s.push(r);
vis[r]=true;
color[r]=0;
ans++; sz++;
while(!s.empty())
{
ll v=s.top();
s.pop();
for(ll i=0; i<(ll)adj[v].size(); i++)
{
if(!vis[adj[v][i]])
{
s.push(adj[v][i]);
color[adj[v][i]]=(1+color[v])%2;
sz++;
if(color[adj[v][i]]==0)
ans++;
}
}
vis[v]=true;
}
return max(sz-ans, ans);
}
int main(void)
{
int t;
cin>>t;
while(t--)
{
ll n, m, ans=0;
bool valid=true;
cin>>n>>m;
vector< pair<ll, ll> > edges;
for(int i=1; i<=n; i++)
adj[i].clear();
memset(vis, 0, sizeof(vis));
memset(color, -1, sizeof(color));
for(int i=0; i<m; i++)
{
ll u, v;
cin>>u>>v;
edges.push_back({u,v});
adj[u].pb(v);
adj[v].pb(u);
}
for(int i=1; i<=n; i++)
{
if(!vis[i])
ans+=dfs(i);
}
for(int i=0; i<m; i++)
{
pair<ll, ll> p;
p=edges[i];
if(color[p.f]==color[p.s])
valid=false;
}
if(!valid)
cout<<"NO\n";
else
cout<<ans<<"\n";
}
return 0;
}