Monk and Graph Problem

来源:互联网 发布:淘宝登入密码怎么修改 编辑:程序博客网 时间:2024/05/29 15:37

中文题意:求一个图中的最大连通子图的边的个数。

Monk and his graph problems never end. Here is one more from our own Monk:
Given an undirected graph with NN vertices and MM edges, what is the maximum number of edges in any connected component of the graph.
In other words, if given graph has kk connected components, and E1,E2,....EkE1,E2,....Ek be the number of edges in the respective connected component, then find max(E1,E2,....,Ek)max(E1,E2,....,Ek) .

The graph may have multiple edges and self loops. The NN vertices are numbered as 1,2,...,N1,2,...,N.

Input Format:
The first line of input consists of two space separated integers NN and MM, denoting number of vertices in the graph and number of edges in the graph respectively.
Following MM lines consists of two space separated each aa and bb, denoting there is an edge between vertex aa and vertex bb.

Output Format:
The only line of output consists of answer of the question asked by Monk.

Input Constraints:
1N1051≤N≤105
0M1050≤M≤105
1a,bN1≤a,b≤N

SAMPLE INPUT
 
6 31 22 34 5
SAMPLE OUTPUT
 
2
Explanation

The graph has 33 connected components :
First component is 1231−2−3 which has 22 edges.
Second component is 454−5 which has 11 edge.
Third component is 66 which has no edges.

So, answer is max(2,1,0)=2max(2,1,0)=2

Time Limit:1.0 sec(s) for each input file.
Memory Limit:256 MB
Source Limit:1024 KB
Marking Scheme:Marks are awarded when all the testcases pass.
Allowed Languages:

C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Visual Basic

#include <bits/stdc++.h>#include <vector>using namespace std;vector <int>adj[100005];bool visited[100005];int mmax = 0;int Count=0;void dfs(int s){visited[s] = 1;for (int i = 0; i<adj[s].size(); ++i){    Count++;if (visited[adj[s][i]]== 0)dfs(adj[s][i]);}}int main(){int nodes, edges, x, y;cin >> nodes >> edges;memset(visited, 0, sizeof(visited));for (int i = 0; i<edges; ++i){cin >> x >> y;adj[x].push_back(y);adj[y].push_back(x);}for (int i=0; i <= nodes; ++i){if (!visited[i])dfs(i);mmax=max(mmax,Count/2);Count=0;}cout << mmax << endl;}