Skip to content

Commit e2566f0

Browse files
committed
[uva] Add C++ solution for 11504 - Dominos.
1 parent 5263dee commit e2566f0

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

uva/11504-1.ans

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

uva/11504-1.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1
2+
3 2
3+
1 2
4+
2 3

uva/11504.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// https://uva.onlinejudge.org/external/115/11504.pdf
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
using vi=vector<int>;
5+
using vvi=vector<vi>;
6+
using qi=queue<int>;
7+
int main(){
8+
ios::sync_with_stdio(0);
9+
cin.tie(0);
10+
int t,n,m,u,v;
11+
cin>>t;
12+
while(t--){
13+
cin>>n>>m;
14+
vvi g(n),h(n),c;
15+
for(int i=0;i<m;i++){
16+
cin>>u>>v;
17+
u--;v--;
18+
g[u].push_back(v);
19+
h[v].push_back(u);
20+
}
21+
vi a,s(n),t(n),r(n);
22+
function<void(int)>dfs1=[&](int i){
23+
s[i]=1;
24+
for(int j:g[i])
25+
if(!s[j])dfs1(j);
26+
a.push_back(i);
27+
};
28+
for(int i=0;i<n;i++)
29+
if(!s[i])dfs1(i);
30+
reverse(a.begin(),a.end());
31+
function<void(int)>dfs2=[&](int i){
32+
t[i]=1;
33+
c.back().push_back(i);
34+
for(int j:h[i])
35+
if(!t[j])dfs2(j);
36+
};
37+
for(int i=0;i<a.size();i++)
38+
if(!t[a[i]]){
39+
c.push_back(vi());
40+
dfs2(a[i]);
41+
}
42+
function<void(int)>dfs3=[&](int i){
43+
r[i]=1;
44+
for(int j:g[i])
45+
if(!r[j])dfs3(j);
46+
};
47+
int k=0;
48+
for(vi &x:c)
49+
if(!r[x[0]]){
50+
k++;
51+
dfs3(x[0]);
52+
}
53+
cout<<k<<"\n";
54+
}
55+
}

uva/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ There are solutions for the following
111111
([problem site](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2409))
112112
1. [11503 - Virtual Friends](11503.cc)
113113
([problem site](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2498))
114+
1. [11504 - Dominos](11504.cc)
115+
([problem site](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2499))
114116
1. [11518 - Dominos 2](11518.cc)
115117
([problem site](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2513))
116118
1. [11550 - Demanding Dillema](11550.cc)

0 commit comments

Comments
 (0)