Skip to content

Commit 10a9000

Browse files
committed
update
1 parent b6ca39e commit 10a9000

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

CP Templetes/Graphs/BFS.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ void bfs(int firstnode, unordered_map<int, bool>& visited)
2424
// if(!visited[i]) {
2525
// g.bfs(i, visited);
2626
// }
27-
// }
27+
// }
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#ifdef ONLINE_JUDGE
4+
#define DISABLE_STACK_SIZE_CHANGE
5+
#endif
6+
#ifndef DISABLE_STACK_SIZE_CHANGE
7+
#include <sys/resource.h>
8+
#endif
9+
typedef long long ll;
10+
const ll MOD = 1000000007;
11+
#define pb push_back
12+
#define ff first
13+
#define ss second
14+
#define nl "\n"
15+
#define set_bits(x) __builtin_popcountll(x)
16+
#define all(x) (x).begin(), (x).end()
17+
#define debug(x) cerr<<#x<<" "<<x<<endl;
18+
#define loop(i,a,b) for(int i=(a);i<(b);i++)
19+
#define print(x) for(auto it:(x)) cout<<(it)<<" "; cout<<endl;
20+
#define showadj for (auto it : adj) { cout << it.ff << " ->"; for (auto i : it.ss) cout << i << " "; cout << endl; }
21+
template<typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
22+
template<typename T> T binpow(T base,T power,T mod){ ll ans=1; base = base % mod;while(power){if(power&1) ans=(ans*base)%mod; base=((base*base)%mod); power>>=1;}return ans;}
23+
24+
bool dfs(int node,unordered_map<int,bool>&visi,unordered_map<int,vector<int>>&adj,int parent,unordered_map<int,bool>&dfsvisi)
25+
{
26+
visi[node]=true;
27+
dfsvisi[node]=true;
28+
for(auto nbr:adj[node])
29+
{
30+
if(!visi[nbr])
31+
{
32+
bool ans=dfs(nbr,visi,adj,node,dfsvisi);
33+
if(ans)return true;
34+
}
35+
else
36+
{
37+
if(dfsvisi[nbr])return true;
38+
}
39+
}
40+
dfsvisi[node]=false;
41+
return false;
42+
}
43+
44+
int main()
45+
{
46+
ios_base::sync_with_stdio(0);cin.tie(0);
47+
#ifndef DISABLE_STACK_SIZE_CHANGE
48+
rlimit rlim;
49+
if (getrlimit(RLIMIT_STACK, &rlim) != 0) {return 1;}
50+
rlim.rlim_cur = 1024 * 1024 * 1024;
51+
if (setrlimit(RLIMIT_STACK, &rlim) != 0) {return 2;}
52+
#endif
53+
54+
int n;
55+
cin>>n;
56+
unordered_map<int,vector<int>>adj;
57+
unordered_map<int,bool>visi;
58+
unordered_map<int,bool>dfsvisi;
59+
loop(i,0,n)
60+
{
61+
int u,v;
62+
cin>>u>>v;
63+
adj[u].pb(v);
64+
}
65+
loop(i,0,n+1)
66+
{
67+
if(!visi[i])
68+
{
69+
bool ans=dfs(i,visi,adj,0,dfsvisi);
70+
if(ans)
71+
{
72+
cout<<"cycle"<<endl;
73+
return 0;
74+
break;
75+
}
76+
}
77+
}
78+
cout<<"no"<<endl;
79+
}
80+

Important.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@
1414
14->int sum=accumulate(v.begin(),v.end(),0(initial sum value)); to get sum of vector;
1515
15-> transform(s.begin(), s.end(), s.begin(), ::tolower); to make string lower ;
1616
sweepline adhoc
17-
16->s.erase(unique(s.begin(), s.end()), s.end()); to remove all duplicates of string or vector;
17+
16->s.erase(unique(s.begin(), s.end()), s.end()); to remove all duplicates of string or vector;
18+
17->print
19+
vector<pair<int, int>> e(n);
20+
for (auto [x, y]: e) {
21+
cerr << x << ' ' << y << '\n';
22+
}
23+
cerr << '\n';

0 commit comments

Comments
 (0)