mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-29 15:36:06 +08:00
style: format code
This commit is contained in:
@@ -1,59 +1,72 @@
|
||||
#include<bits/stdc++.h>
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
class graph{
|
||||
class graph
|
||||
{
|
||||
int v;
|
||||
list<int> *adj;
|
||||
public:
|
||||
|
||||
public:
|
||||
graph(int v);
|
||||
void addedge(int src,int dest);
|
||||
void addedge(int src, int dest);
|
||||
void printgraph();
|
||||
void bfs(int s);
|
||||
};
|
||||
graph::graph(int v){
|
||||
graph::graph(int v)
|
||||
{
|
||||
this->v = v;
|
||||
this->adj = new list<int>[v];
|
||||
}
|
||||
void graph::addedge(int src,int dest){
|
||||
src--;dest--;
|
||||
void graph::addedge(int src, int dest)
|
||||
{
|
||||
src--;
|
||||
dest--;
|
||||
adj[src].push_back(dest);
|
||||
//adj[dest].push_back(src);
|
||||
}
|
||||
void graph::printgraph(){
|
||||
for(int i=0;i<this->v;i++){
|
||||
cout<<"Adjacency list of vertex "<<i+1<<" is \n";
|
||||
void graph::printgraph()
|
||||
{
|
||||
for (int i = 0; i < this->v; i++)
|
||||
{
|
||||
cout << "Adjacency list of vertex " << i + 1 << " is \n";
|
||||
list<int>::iterator it;
|
||||
for(it=adj[i].begin();it!=adj[i].end();++it){
|
||||
cout<<*it+1<<" ";
|
||||
for (it = adj[i].begin(); it != adj[i].end(); ++it)
|
||||
{
|
||||
cout << *it + 1 << " ";
|
||||
}
|
||||
cout<<endl;
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
void graph::bfs(int s){
|
||||
bool *visited = new bool[this->v+1];
|
||||
memset(visited,false,sizeof(bool)*(this->v+1));
|
||||
visited[s]=true;
|
||||
void graph::bfs(int s)
|
||||
{
|
||||
bool *visited = new bool[this->v + 1];
|
||||
memset(visited, false, sizeof(bool) * (this->v + 1));
|
||||
visited[s] = true;
|
||||
list<int> q;
|
||||
q.push_back(s);
|
||||
list<int>::iterator it;
|
||||
while(!q.empty()){
|
||||
while (!q.empty())
|
||||
{
|
||||
int u = q.front();
|
||||
cout<<u<<" ";
|
||||
cout << u << " ";
|
||||
q.pop_front();
|
||||
for(it=adj[u].begin();it!=adj[u].end();++it){
|
||||
if(visited[*it]==false){
|
||||
for (it = adj[u].begin(); it != adj[u].end(); ++it)
|
||||
{
|
||||
if (visited[*it] == false)
|
||||
{
|
||||
visited[*it] = true;
|
||||
q.push_back(*it);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
int main()
|
||||
{
|
||||
graph g(4);
|
||||
g.addedge(1,2);
|
||||
g.addedge(2,3);
|
||||
g.addedge(3,4);
|
||||
g.addedge(1,4);
|
||||
g.addedge(1,3);
|
||||
g.addedge(1, 2);
|
||||
g.addedge(2, 3);
|
||||
g.addedge(3, 4);
|
||||
g.addedge(1, 4);
|
||||
g.addedge(1, 3);
|
||||
//g.printgraph();
|
||||
g.bfs(2);
|
||||
return 0;
|
||||
|
||||
@@ -1,27 +1,31 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int v = 4;
|
||||
void DFSUtil_(int graph[4][4],bool visited[],int s){
|
||||
void DFSUtil_(int graph[4][4], bool visited[], int s)
|
||||
{
|
||||
visited[s] = true;
|
||||
cout<<s<<" ";
|
||||
for(int i=0;i<v;i++){
|
||||
if(graph[s][i]==1&&visited[i]==false){
|
||||
DFSUtil_(graph,visited,i);
|
||||
cout << s << " ";
|
||||
for (int i = 0; i < v; i++)
|
||||
{
|
||||
if (graph[s][i] == 1 && visited[i] == false)
|
||||
{
|
||||
DFSUtil_(graph, visited, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DFS_(int graph[4][4],int s){
|
||||
void DFS_(int graph[4][4], int s)
|
||||
{
|
||||
bool visited[v];
|
||||
memset(visited,0,sizeof(visited));
|
||||
DFSUtil_(graph,visited,s);
|
||||
memset(visited, 0, sizeof(visited));
|
||||
DFSUtil_(graph, visited, s);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int graph[4][4] = {{0,1,1,0},{0,0,1,0},{1,0,0,1},{0,0,0,1}};
|
||||
cout<<"DFS: ";
|
||||
DFS_(graph,2);
|
||||
cout<<endl;
|
||||
return 0;
|
||||
int graph[4][4] = {{0, 1, 1, 0}, {0, 0, 1, 0}, {1, 0, 0, 1}, {0, 0, 0, 1}};
|
||||
cout << "DFS: ";
|
||||
DFS_(graph, 2);
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
#include <list>
|
||||
#include <stack>
|
||||
|
||||
|
||||
#define WHITE 0
|
||||
#define GREY 1
|
||||
#define BLACK 2
|
||||
@@ -12,23 +11,27 @@ using namespace std;
|
||||
|
||||
int checked[999] = {WHITE};
|
||||
|
||||
void dfs(const list<int> lista[], int start) {
|
||||
void dfs(const list<int> lista[], int start)
|
||||
{
|
||||
stack<int> stack;
|
||||
|
||||
int checked[999] = {WHITE};
|
||||
|
||||
|
||||
stack.push(start);
|
||||
|
||||
|
||||
checked[start] = GREY;
|
||||
while(!stack.empty()) {
|
||||
while (!stack.empty())
|
||||
{
|
||||
int act = stack.top();
|
||||
stack.pop();
|
||||
|
||||
if(checked[act] == GREY) {
|
||||
if (checked[act] == GREY)
|
||||
{
|
||||
cout << act << ' ';
|
||||
for(auto it = lista[act].begin(); it != lista[act].end(); ++it) {
|
||||
for (auto it = lista[act].begin(); it != lista[act].end(); ++it)
|
||||
{
|
||||
stack.push(*it);
|
||||
if(checked[*it] != BLACK)
|
||||
if (checked[*it] != BLACK)
|
||||
checked[*it] = GREY;
|
||||
}
|
||||
checked[act] = BLACK; //nodo controllato
|
||||
@@ -36,18 +39,19 @@ void dfs(const list<int> lista[], int start) {
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int u, w;
|
||||
int n;
|
||||
cin >> n;
|
||||
list<int> lista[INF];
|
||||
for(int i = 0; i < n; ++i) {
|
||||
cin >> u >> w;
|
||||
lista[u].push_back(w);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int u, w;
|
||||
int n;
|
||||
cin >> n;
|
||||
list<int> lista[INF];
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
cin >> u >> w;
|
||||
lista[u].push_back(w);
|
||||
}
|
||||
|
||||
dfs(lista, 0);
|
||||
|
||||
dfs(lista, 0);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -4,46 +4,52 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define INF 10000010
|
||||
vector < pair <int,int> > graph[5*100001];
|
||||
int dis[5*100001];
|
||||
int dij(vector <pair<int,int> > * v,int s,int * dis) {
|
||||
priority_queue < pair <int,int> , vector < pair <int,int> >,greater < pair <int,int> > > pq;
|
||||
vector<pair<int, int>> graph[5 * 100001];
|
||||
int dis[5 * 100001];
|
||||
int dij(vector<pair<int, int>> *v, int s, int *dis)
|
||||
{
|
||||
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
|
||||
// source distance to zero.
|
||||
pq.push(make_pair(0,s));
|
||||
pq.push(make_pair(0, s));
|
||||
dis[s] = 0;
|
||||
int u;
|
||||
while(!pq.empty()) {
|
||||
while (!pq.empty())
|
||||
{
|
||||
u = (pq.top()).second;
|
||||
pq.pop();
|
||||
for( vector <pair <int,int> > :: iterator it = v[u].begin(); it != v[u].end();it++) {
|
||||
if(dis[u] + it->first < dis[it->second]) {
|
||||
for (vector<pair<int, int>>::iterator it = v[u].begin(); it != v[u].end(); it++)
|
||||
{
|
||||
if (dis[u] + it->first < dis[it->second])
|
||||
{
|
||||
dis[it->second] = dis[u] + it->first;
|
||||
pq.push(make_pair(dis[it->second],it->second));
|
||||
pq.push(make_pair(dis[it->second], it->second));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int m,n,l,x,y,s;
|
||||
int main()
|
||||
{
|
||||
int m, n, l, x, y, s;
|
||||
// n--> number of nodes , m --> number of edges
|
||||
cin>>n>>m;
|
||||
for(int i = 0;i < m;i++) {
|
||||
cin >> n >> m;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
// input edges.
|
||||
scanf("%d%d%d",&x,&y,&l);
|
||||
graph[x].push_back(make_pair(l,y));
|
||||
graph[y].push_back(make_pair(l,x)); // comment this line for directed graph
|
||||
scanf("%d%d%d", &x, &y, &l);
|
||||
graph[x].push_back(make_pair(l, y));
|
||||
graph[y].push_back(make_pair(l, x)); // comment this line for directed graph
|
||||
}
|
||||
// start node.
|
||||
scanf("%d",&s);
|
||||
scanf("%d", &s);
|
||||
// intialise all distances to infinity.
|
||||
for(int i = 1;i <= n;i++)
|
||||
for (int i = 1; i <= n; i++)
|
||||
dis[i] = INF;
|
||||
dij(graph,s,dis);
|
||||
|
||||
for(int i = 1;i <= n;i++)
|
||||
if(dis[i] == INF)
|
||||
cout<<"-1 ";
|
||||
dij(graph, s, dis);
|
||||
|
||||
for (int i = 1; i <= n; i++)
|
||||
if (dis[i] == INF)
|
||||
cout << "-1 ";
|
||||
else
|
||||
cout<<dis[i]<<" ";
|
||||
cout << dis[i] << " ";
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,91 +1,134 @@
|
||||
#include "bits/stdc++.h"
|
||||
//#include <boost/multiprecision/cpp_int.hpp>
|
||||
//using namespace boost::multiprecision;
|
||||
const int mx = 1e6+5;
|
||||
const int mx = 1e6 + 5;
|
||||
const long int inf = 2e9;
|
||||
typedef long long ll;
|
||||
#define rep(i,n) for(i=0;i<n;i++)
|
||||
#define repp(i,a,b) for(i=a;i<=b;i++)
|
||||
#define pii pair<int,int>
|
||||
#define vpii vector< pii >
|
||||
#define rep(i, n) for (i = 0; i < n; i++)
|
||||
#define repp(i, a, b) for (i = a; i <= b; i++)
|
||||
#define pii pair<int, int>
|
||||
#define vpii vector<pii>
|
||||
#define vi vector<int>
|
||||
#define vll vector<ll>
|
||||
#define r(x) scanf("%d",&x)
|
||||
#define rs(s) scanf("%s",s)
|
||||
#define vll vector<ll>
|
||||
#define r(x) scanf("%d", &x)
|
||||
#define rs(s) scanf("%s", s)
|
||||
#define gc getchar_unlocked
|
||||
#define pc putchar_unlocked
|
||||
#define mp make_pair
|
||||
#define pb push_back
|
||||
#define pb push_back
|
||||
#define lb lower_bound
|
||||
#define ub upper_bound
|
||||
#define endl "\n"
|
||||
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
|
||||
#define fast \
|
||||
ios_base::sync_with_stdio(false); \
|
||||
cin.tie(NULL); \
|
||||
cout.tie(NULL);
|
||||
using namespace std;
|
||||
void in(int &x)
|
||||
{
|
||||
register int c = gc(); x = 0; int neg = 0;
|
||||
for(;((c<48 || c>57) && c != '-');c = gc());if(c=='-') {neg=1;c=gc();} for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
|
||||
if(neg) x=-x;
|
||||
register int c = gc();
|
||||
x = 0;
|
||||
int neg = 0;
|
||||
for (; ((c < 48 || c > 57) && c != '-'); c = gc())
|
||||
;
|
||||
if (c == '-')
|
||||
{
|
||||
neg = 1;
|
||||
c = gc();
|
||||
}
|
||||
for (; c > 47 && c < 58; c = gc())
|
||||
{
|
||||
x = (x << 1) + (x << 3) + c - 48;
|
||||
}
|
||||
if (neg)
|
||||
x = -x;
|
||||
}
|
||||
void out (int n) { int N = n, rev, count = 0;rev = N; if (N == 0) { pc('0'); return ;} while ((rev % 10) == 0) { count++; rev /= 10;} rev = 0;while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}while (rev != 0) { pc(rev % 10 + '0'); rev /= 10;}
|
||||
while (count--) pc('0');
|
||||
void out(int n)
|
||||
{
|
||||
int N = n, rev, count = 0;
|
||||
rev = N;
|
||||
if (N == 0)
|
||||
{
|
||||
pc('0');
|
||||
return;
|
||||
}
|
||||
while ((rev % 10) == 0)
|
||||
{
|
||||
count++;
|
||||
rev /= 10;
|
||||
}
|
||||
rev = 0;
|
||||
while (N != 0)
|
||||
{
|
||||
rev = (rev << 3) + (rev << 1) + N % 10;
|
||||
N /= 10;
|
||||
}
|
||||
while (rev != 0)
|
||||
{
|
||||
pc(rev % 10 + '0');
|
||||
rev /= 10;
|
||||
}
|
||||
while (count--)
|
||||
pc('0');
|
||||
}
|
||||
ll parent[mx],arr[mx],node,edge;
|
||||
vector<pair<ll,pair<ll,ll>>>v;
|
||||
ll parent[mx], arr[mx], node, edge;
|
||||
vector<pair<ll, pair<ll, ll>>> v;
|
||||
void initial()
|
||||
{
|
||||
int i;
|
||||
rep(i,node+edge)
|
||||
rep(i, node + edge)
|
||||
parent[i] = i;
|
||||
}
|
||||
int root(int i)
|
||||
{
|
||||
while(parent[i] != i)
|
||||
while (parent[i] != i)
|
||||
{
|
||||
parent[i] = parent[parent[i]];
|
||||
i = parent[i];
|
||||
}
|
||||
return i;
|
||||
}
|
||||
void join(int x,int y)
|
||||
void join(int x, int y)
|
||||
{
|
||||
int root_x = root(x); //Disjoint set union by rank
|
||||
int root_x = root(x); //Disjoint set union by rank
|
||||
int root_y = root(y);
|
||||
parent[root_x] = root_y;
|
||||
}
|
||||
ll kruskal()
|
||||
{
|
||||
ll mincost=0,i,x,y;
|
||||
rep(i,edge)
|
||||
ll mincost = 0, i, x, y;
|
||||
rep(i, edge)
|
||||
{
|
||||
x = v[i].second.first;
|
||||
y = v[i].second.second;
|
||||
if(root(x) != root(y))
|
||||
if (root(x) != root(y))
|
||||
{
|
||||
mincost += v[i].first;
|
||||
join(x,y);
|
||||
join(x, y);
|
||||
}
|
||||
}
|
||||
return mincost;
|
||||
}
|
||||
int main(){
|
||||
int main()
|
||||
{
|
||||
fast;
|
||||
while(1)
|
||||
while (1)
|
||||
{
|
||||
int i,j,from,to,cost,totalcost=0;
|
||||
cin>>node>>edge; //Enter the nodes and edges
|
||||
if(node==0 && edge==0) break; //Enter 0 0 to break out
|
||||
initial(); //Initialise the parent array
|
||||
rep(i,edge)
|
||||
int i, j, from, to, cost, totalcost = 0;
|
||||
cin >> node >> edge; //Enter the nodes and edges
|
||||
if (node == 0 && edge == 0)
|
||||
break; //Enter 0 0 to break out
|
||||
initial(); //Initialise the parent array
|
||||
rep(i, edge)
|
||||
{
|
||||
cin>>from>>to>>cost;
|
||||
v.pb(mp(cost,mp(from,to)));
|
||||
cin >> from >> to >> cost;
|
||||
v.pb(mp(cost, mp(from, to)));
|
||||
totalcost += cost;
|
||||
}
|
||||
sort(v.begin(),v.end());
|
||||
sort(v.begin(), v.end());
|
||||
// rep(i,v.size())
|
||||
// cout<<v[i].first<<" ";
|
||||
cout<<kruskal()<<endl;
|
||||
cout << kruskal() << endl;
|
||||
v.clear();
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -1,44 +1,51 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
using namespace std;
|
||||
|
||||
int n , m; // For number of Vertices (V) and number of edges (E)
|
||||
vector< vector<int> > G;
|
||||
int n, m; // For number of Vertices (V) and number of edges (E)
|
||||
vector<vector<int>> G;
|
||||
vector<bool> visited;
|
||||
vector<int> ans;
|
||||
|
||||
void dfs(int v) {
|
||||
void dfs(int v)
|
||||
{
|
||||
visited[v] = true;
|
||||
for (int u : G[v]) {
|
||||
for (int u : G[v])
|
||||
{
|
||||
if (!visited[u])
|
||||
dfs(u);
|
||||
}
|
||||
ans.push_back(v);
|
||||
}
|
||||
|
||||
void topological_sort() {
|
||||
|
||||
void topological_sort()
|
||||
{
|
||||
visited.assign(n, false);
|
||||
ans.clear();
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
if (!visited[i])
|
||||
dfs(i);
|
||||
}
|
||||
reverse(ans.begin(), ans.end());
|
||||
}
|
||||
int main(){
|
||||
int main()
|
||||
{
|
||||
cout << "Enter the number of vertices and the number of directed edges\n";
|
||||
cin >> n >> m;
|
||||
int x , y;
|
||||
G.resize(n , vector<int>());
|
||||
for(int i = 0 ; i < n ; ++i) {
|
||||
int x, y;
|
||||
G.resize(n, vector<int>());
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
cin >> x >> y;
|
||||
x-- , y--; // to convert 1-indexed to 0-indexed
|
||||
x--, y--; // to convert 1-indexed to 0-indexed
|
||||
G[x].push_back(y);
|
||||
}
|
||||
topological_sort();
|
||||
cout << "Topological Order : \n";
|
||||
for(int v : ans) {
|
||||
for (int v : ans)
|
||||
{
|
||||
cout << v + 1 << ' '; // converting zero based indexing back to one based.
|
||||
}
|
||||
cout << '\n';
|
||||
|
||||
Reference in New Issue
Block a user