-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdominos_UVA11504_2.cpp
143 lines (119 loc) · 3.1 KB
/
dominos_UVA11504_2.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("dominos_UVA11504.in");
ofstream fout("dominos_UVA11504.out");
struct in
{
int first;
int second;
};
vector<in> input;
int finalAnswer;
void tarjan(int u, int n, int & index, vector<int> & dfn, vector<int> & low, vector<bool> & inStack, vector<vector<bool>> & a, stack<int> & stk)
{
++index;
dfn[u] = index; low[u] = index;
stk.push(u); inStack[u] = true;
int ans = 0;
for (int v = 1; v <= n; ++v)
{
if (a[u][v] == true)
{
if (dfn[v] == -1)
{
tarjan(v, n, index, dfn, low, inStack, a, stk);
low[u] = min(low[u], low[v]);
}
else if (inStack[v] == true)
{
low[u] = min(low[u], dfn[v]);
}
}
}
if (dfn[u] == low[u])
{
vector<bool> inGroup(n + 1, false);
int t;
vector<int> temp;
//vector<int> temp2(n,-1);
do
{
t = stk.top();
temp.push_back(t);
//temp2[t]=0;
inStack[t] = false;
inGroup[t] = true;
stk.pop();
}
while (t != u);
bool ok = true;
// int size = input.size();
// for (int i = 0; i <= size - 1; ++i)
// {
// if (inGroup[input[i].first] == false && inGroup[input[i].second] == true)
// {
// ok = false;
// break;
// }
// }
int size = temp.size();
for (int i = 0; i <= size - 1; ++i)
{
for (int j = 1; j <= n; ++j)
{
if(a[j][temp[i]]==true && inGroup[input[i].first] == false)
{
ok = false;
break;
}
}
}
if (ok == true)
{
++finalAnswer;
}
}
}
int main()
{
double totalTime=0;
int testCase; fin >> testCase;
for (int c = 1; c <= testCase; ++c)
{
int n, m; fin >> n >> m;
vector<vector<bool>> a(n + 1, vector<bool>(n + 1, false));
input.clear();
finalAnswer = 0;
for (int k = 1; k <= m; ++k)
{
int u, v; fin >> u >> v;
a[u][v] = true;
in temp{u, v};
input.push_back(temp);
}
int zero = 0;
vector<int> dfn(n + 1, -1), low(n + 1, -1);
vector<bool> inStack(n + 1, false);
stack<int> stk;
vector<vector<int>> res;
for (int i = 1; i <= n; ++i)
{
if (dfn[i] == -1)
{
tarjan(i, n, zero, dfn, low, inStack, a, stk);
}
}
fout << finalAnswer << '\n';
}
return 0;
}