Skip to content

Commit 3bd4567

Browse files
committed
Euler Tour & MIsc
1 parent e488a19 commit 3bd4567

File tree

3 files changed

+340
-0
lines changed

3 files changed

+340
-0
lines changed

Algorithm/48 Euler Tour.cpp

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
2+
/**
3+
4+
Euler Circuit / Euler Cycle (Undirected graph)
5+
===========================
6+
All edges are traversed only once . Starting & Ending Point same.
7+
8+
Condition : All vertices will have EVEN degree .
9+
10+
Euler Path
11+
==========
12+
All edges are traversed only once . Starting & Ending Point different.
13+
14+
Condition : All vertices except the 2 endpoints will have EVEN degree .
15+
16+
17+
18+
Hierholzer's algorithm for finding Euler Circuit
19+
=================================================
20+
https://www.wikiwand.com/en/Eulerian_path#/Hierholzer.27s_algorithm
21+
22+
How to modify the same algorithm to find Euler Path ?
23+
=> Add an edge between the 2 endpoints and find Euler Circuit
24+
25+
**/
26+
27+
/** Which of the favors of your Lord will you deny ? **/
28+
29+
#include<bits/stdc++.h>
30+
using namespace std;
31+
32+
#define LL long long
33+
#define PII pair<int,int>
34+
#define PLL pair<LL,LL>
35+
#define F first
36+
#define S second
37+
38+
#define ALL(x) (x).begin(), (x).end()
39+
#define READ freopen("alu.txt", "r", stdin)
40+
#define WRITE freopen("vorta.txt", "w", stdout)
41+
42+
#ifndef ONLINE_JUDGE
43+
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
44+
#else
45+
#define DBG(x)
46+
#endif
47+
48+
template<class T1, class T2>
49+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
50+
template <class T>
51+
ostream &operator <<(ostream &os, vector<T>&v);
52+
template <class T>
53+
ostream &operator <<(ostream &os, set<T>&v);
54+
55+
inline void optimizeIO()
56+
{
57+
ios_base::sync_with_stdio(false);
58+
cin.tie(NULL);
59+
}
60+
61+
const int nmax = 2e5+7;
62+
63+
vector<set<int>>adj;
64+
vector<int>deg;
65+
vector<int>tour;
66+
67+
void eulerDFS(int u)
68+
{
69+
while(!adj[u].empty())
70+
{
71+
int v = *adj[u].begin();
72+
73+
adj[u].erase(v);
74+
adj[v].erase(u);
75+
76+
eulerDFS(v);
77+
}
78+
79+
tour.push_back(u);
80+
}
81+
82+
bool check(int n) /// check if Euler Circuit Exists
83+
{
84+
for(int i=1;i<=n;i++)
85+
if(deg[i]&1)
86+
return false;
87+
88+
return true;
89+
}
90+
91+
int main()
92+
{
93+
optimizeIO();
94+
95+
int tc;
96+
cin>>tc;
97+
98+
while(tc--)
99+
{
100+
int n,m;
101+
cin>>n>>m;
102+
103+
adj = vector<set<int>>(n+1);
104+
deg = vector<int>(n+1);
105+
tour.clear();
106+
107+
while(m--)
108+
{
109+
int a , b;
110+
cin>>a>>b;
111+
112+
adj[a].insert(b);
113+
adj[b].insert(a);
114+
115+
deg[a]++;
116+
deg[b]++;
117+
}
118+
119+
if(check(n)==false) cout<<"Euler Circuit doesn't exist"<<endl;
120+
else
121+
{
122+
eulerDFS(1);
123+
124+
reverse(ALL(tour));
125+
cout<<"Euler Circuit : "<<tour<<endl;
126+
}
127+
}
128+
129+
return 0;
130+
}
131+
132+
/**
133+
7 8
134+
1 4
135+
1 2
136+
2 3
137+
3 7
138+
3 5
139+
3 4
140+
5 6
141+
6 7
142+
**/
143+
144+
template<class T1, class T2>
145+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
146+
{
147+
os<<"{"<<p.first<<", "<<p.second<<"} ";
148+
return os;
149+
}
150+
template <class T>
151+
ostream &operator <<(ostream &os, vector<T>&v)
152+
{
153+
os<<"[ ";
154+
for(int i=0; i<v.size(); i++)
155+
{
156+
os<<v[i]<<" " ;
157+
}
158+
os<<" ]";
159+
return os;
160+
}
161+
162+
template <class T>
163+
ostream &operator <<(ostream &os, set<T>&v)
164+
{
165+
os<<"[ ";
166+
for(T i:v)
167+
{
168+
os<<i<<" ";
169+
}
170+
os<<" ]";
171+
return os;
172+
}
173+
174+
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
/** Which of the favors of your Lord will you deny ? **/
3+
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
#define LL long long
8+
#define PII pair<int,int>
9+
#define PLL pair<LL,LL>
10+
#define F first
11+
#define S second
12+
13+
#define ALL(x) (x).begin(), (x).end()
14+
#define READ freopen("alu.txt", "r", stdin)
15+
#define WRITE freopen("vorta.txt", "w", stdout)
16+
17+
#ifndef ONLINE_JUDGE
18+
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
19+
#else
20+
#define DBG(x)
21+
#endif
22+
23+
template<class T1, class T2>
24+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
25+
template <class T>
26+
ostream &operator <<(ostream &os, vector<T>&v);
27+
template <class T>
28+
ostream &operator <<(ostream &os, set<T>&v);
29+
30+
inline void optimizeIO()
31+
{
32+
ios_base::sync_with_stdio(false);
33+
cin.tie(NULL);
34+
}
35+
36+
const int nmax = 2e5+7;
37+
38+
LL sqroot(LL x)
39+
{
40+
LL l = 0, r = 1e9 + 1;
41+
while (l < r - 1)
42+
{
43+
LL m = (l + r) / 2;
44+
if (m * m > x)
45+
r = m;
46+
else
47+
l = m;
48+
}
49+
return l;
50+
}
51+
52+
int main()
53+
{
54+
optimizeIO();
55+
56+
while(1)
57+
{
58+
LL x;
59+
cin>>x;
60+
61+
cout<<sqroot(x)<<endl;
62+
}
63+
64+
return 0;
65+
}
66+
67+
/**
68+
69+
**/
70+
71+
template<class T1, class T2>
72+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
73+
{
74+
os<<"{"<<p.first<<", "<<p.second<<"} ";
75+
return os;
76+
}
77+
template <class T>
78+
ostream &operator <<(ostream &os, vector<T>&v)
79+
{
80+
os<<"[ ";
81+
for(int i=0; i<v.size(); i++)
82+
{
83+
os<<v[i]<<" " ;
84+
}
85+
os<<" ]";
86+
return os;
87+
}
88+
89+
template <class T>
90+
ostream &operator <<(ostream &os, set<T>&v)
91+
{
92+
os<<"[ ";
93+
for(T i:v)
94+
{
95+
os<<i<<" ";
96+
}
97+
os<<" ]";
98+
return os;
99+
}
100+
101+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
/**
3+
4+
Code : mochow13
5+
6+
**/
7+
8+
unsigned int fast_sqrt(unsigned int n){
9+
unsigned int c, g;
10+
11+
c = g = 0x8000;
12+
for (; ;){
13+
if ((g * g) > n) g ^= c;
14+
c >>= 1;
15+
if (!c) return g;
16+
g |= c;
17+
}
18+
}
19+
20+
int fast_cbrt(int n){
21+
int x, r = 30, res = 0;
22+
23+
for (; r >= 0; r -= 3){
24+
res <<= 1;
25+
x = (3 * res * (res + 1)) + 1;
26+
if ((n >> r) >= x){
27+
res++;
28+
n -= (x << r);
29+
}
30+
}
31+
32+
return res;
33+
}
34+
35+
unsigned long long fast_sqrt(unsigned long long n){
36+
unsigned long long c, g;
37+
38+
c = g = 0x80000000;
39+
for (; ;){
40+
if ((g * g) > n) g ^= c;
41+
c >>= 1;
42+
if (!c) return g;
43+
g |= c;
44+
}
45+
}
46+
47+
unsigned long long fast_cbrt(unsigned long long n){
48+
int r = 63;
49+
unsigned long long x, res = 0;
50+
51+
for (; r >= 0; r -= 3){
52+
res <<= 1;
53+
x = (res * (res + 1) * 3) + 1;
54+
if ((n >> r) >= x){
55+
res++;
56+
n -= (x << r);
57+
}
58+
}
59+
60+
return res;
61+
}
62+
63+
int main(){
64+
65+
}

0 commit comments

Comments
 (0)