let's start Code

315 - Network

315 - Network

Topic : Graph Theory(AP)

Solution 01:



 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
#include<bits/stdc++.h>
using namespace std;

#define Max 100000
vector<int> graph[Max];
int parent[Max];
int low[Max];
int d[Max];
int visited[Max];
bool isArticulationPoint[Max];
int Time = 0;

void dfs(int u, int root)
{
    Time = Time + 1;
    visited[u] = Time;
    d[u] = low[u] = Time;
    int noOfChildren = 0;
    for(int i = 0; i <graph[u].size(); i++){
        int v = graph[u][i];
        if(v == parent[u])continue;
        parent[v] = u;
        if(visited[v]) low[u] = min(low[u], d[v]);
        else{
            noOfChildren = noOfChildren + 1;
            dfs(v, root);
            low[u] = min(low[u], low[v]);
            if(low[v] >= d[u] and u != root)isArticulationPoint[u] = true;
        }
    }
    if(u == root and noOfChildren > 1)isArticulationPoint[u] = true;
}

int main()
{
    int n,x,y;
    char c;
    while(cin >> n && n)
    {
        Time = 0;
        for(int t = 0; t <= n; t++){
            graph[t].clear();
            parent[t]= low[t] = d[t] = visited[t] = 0;
            isArticulationPoint[t] = false;
        }
        while(scanf("%d", &x) == 1 && x)
        {
            while(scanf("%d%c", &y, &c) == 2)
            {
                graph[x].push_back(y);
                    graph[y].push_back(x);
                if(c == '\n') break;
            }
        }
        dfs(1, 1);
        cout << count(isArticulationPoint+1, isArticulationPoint+n+1, true)<<endl;
    }
    return 0;
}

Share:

No comments:

Post a Comment

About

let's start CODE

Popular Posts