-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.cpp
174 lines (168 loc) · 4.06 KB
/
graph.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <bits/stdc++.h>
#include <memory>
#define int long long
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
// typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const ll inf = 1e9;
const ll Nmax = 1e6+5;
const ll MOD = 1e9+7;
const long double eps = 1e-18;
template<class T>
T _abs(T a)
{
if(a<0)return -a;else return a;
}
ll _pow(ll x,ll t)
{
if(t==0)return 1;
ll tmp=_pow(x,t/2);
if(t%2)return tmp*tmp%MOD*x%MOD;else return tmp*tmp%MOD;
}
ll _gcd(ll a,ll b)
{
if(b)return _gcd(b,a%b);else return a;
}
bool checkComplete(vector<vector<int> > &adj)
{
int n = adj.size() - 1;
for (int i = 1; i <= n; ++i)
{
if (adj[i].size() != n - 1) return 0;
}
return 1;
}
bool dfs(int u, vector<int> &col, vector<vector<int> > &adj)
{
if (col[u] == -1) col[u] = 0;
for (int i = 0; i < adj[u].size(); ++i)
{
int v = adj[u][i];
if (col[v] != -1 && col[u] == col[v]) return 0;
if (col[v] == -1)
{
col[v] = col[u] ^ 1;
if (dfs(v, col, adj) == 0) return 0;
}
}
return 1;
}
bool checkBi(vector<vector<int> > &adj)
{
int n = adj.size() - 1;
queue<int> q;
vector<int> col(n+1, -1);
for (int i = 1; i <= n; ++i)
{
if (col[i] == -1)
{
if (dfs(i, col, adj) == 0) return 0;
}
}
return 1;
}
bool checkCircular(vector<vector<int> > &adj)
{
int n = adj.size() - 1;
for (int i = 1; i <= n; ++i)
{
if (adj[i].size() != 2) return 0;
}
int cnt = 0, u = 1, pre = -1;
do {
++cnt;
for (int i = 0; i < adj[u].size(); ++i)
{
int v = adj[u][i];
if (v != pre)
{
pre = u;
u = v;
break;
}
}
} while (u != 1);
return (cnt == n);
}
bool checkBiComplete(vector<vector<int> > &adj)
{
int n = adj.size() - 1;
queue<int> q;
vector<int> col(n+1, -1);
for (int i = 1; i <= n; ++i)
{
if (col[i] == -1)
{
if (dfs(i, col, adj) == 0) return 0;
}
}
int cnt1 = 0;
for (int i = 1; i <= n; ++i)
cnt1 += col[i];
for (int i = 1; i <= n; ++i)
{
if (col[i] == 1 && adj[i].size() != (n - cnt1)) return 0;
if (col[i] == 0 && adj[i].size() != cnt1) return 0;
}
return 1;
}
pair<int,int> DFS(int u, vector<vector<int> > &adj, vector<int> &visited)
{
visited[u] = 1;
pair<int,int> res = {1, adj[u].size()};
for (int i = 0; i < adj[u].size(); ++i)
{
int v = adj[u][i];
if (visited[v]) continue;
pair<int,int> tmp = DFS(v, adj, visited);
res.first += tmp.first;
res.second += tmp.second;
}
return res;
}
void countComponent(vector<vector<int> > &adj)
{
int n = adj.size() - 1;
vector<int> visited(n + 1, 0);
int component = 0, tree = 0;
for (int i = 1; i <= n; ++i)
{
if (visited[i]) continue;
pair<int,int> tmp = DFS(i, adj, visited);
++component;
if (tmp.second / 2 == tmp.first - 1) ++tree;
}
cout << "Found " << component << " components and " << tree << " trees\n";
}
int32_t main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("text/input.txt","r",stdin);
freopen("text/output.txt","w",stdout);
#endif //ONLINE_JUDGE
int T=1;
// cin>>T;
while(T--)
{
int n, m;
cin >> n >> m;
vector<vector<int> > adj(n + 1);
for (int i = 0; i < m; ++i)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cout << checkBi(adj) << ": bigraph\n";
cout << checkBiComplete(adj) << ": complete bigraph\n";
cout << checkCircular(adj) << ": circular graph\n";
cout << checkComplete(adj) << ": complete graph\n";
countComponent(adj);
}
return 0;
}