-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
66 lines (56 loc) · 1.5 KB
/
main.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
#include<cstdio>
#include<stack>
#include<vector>
#include"graph.h"
using namespace std;
/*
¥ÑªL«~¾§©M³¯õ¹mÁp¦X½s¼g
made by 40147014S & 40147005S
*/
int main()
{
int T=0;//case Number
scanf(" %d",&T);
for(int t=0;t<T;t+=1)
{
char finalNodeChar;
scanf(" %c",&finalNodeChar);
getchar();
int nodeNum=finalNodeChar-'A'+1;
Graph graph(nodeNum);
bool nodeUsed[26]={0};
//draw grapg
char first,second;//1stNode&2ndNode
while(scanf("%c",&first)!=EOF&&first!='\n')
{
scanf("%c",&second);
graph.AddEdge(first-'A',second-'A');
getchar();
}
int subGraphNum=0;
for(int i=0;i<nodeNum;i+=1)
{
if(nodeUsed[i])continue;
else//DFS
{
subGraphNum+=1;
stack<int> nodeStack;
nodeStack.push(i);
while(!nodeStack.empty())
{
int nodeNow=nodeStack.top();
nodeStack.pop();
if(nodeUsed[nodeNow])continue;
nodeUsed[nodeNow]=true;
vector<int> nextNode=graph.Span(nodeNow);
for(vector<int>::iterator it=nextNode.begin();it!=nextNode.end();it+=1)
{
nodeStack.push(*it);
}
}
}
}
if(t!=0)printf("\n\n");
printf("%d",subGraphNum);
}
}