Skip to content

Commit 5a3081e

Browse files
committed
[uva] Add C++ solution for 11838 - Come and Go.
1 parent e5ceedb commit 5a3081e

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

uva/11838-1.ans

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

uva/11838-1.in

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
4 5
2+
1 2 1
3+
1 3 2
4+
2 4 1
5+
3 4 1
6+
4 1 2
7+
3 2
8+
1 2 2
9+
1 3 2
10+
3 2
11+
1 2 2
12+
1 3 1
13+
4 2
14+
1 2 2
15+
3 4 2
16+
0 0

uva/11838.cc

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

uva/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ There are solutions for the following
2121
([contest site](http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1055))
2222
* [11679 - Sub-prime](11679.cc)
2323
([contest site](http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2726))
24+
* [11838 - Come and Go](11838.cc)
25+
([contest site](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=2938))

0 commit comments

Comments
 (0)