Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
33arsenic75 committed Jun 9, 2024
1 parent ee8ca71 commit 5ba2c75
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 0 deletions.
59 changes: 59 additions & 0 deletions problem-of-the-day/day12/Building Teams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# include <bits/stdc++.h>
# define ll long long
# define all(x) x.begin(), x.end()
# define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
# define MOD 1000000007
using namespace std;

int n,m;
vector <vector<int>> adList;
vector <bool> visit;
vector<int> p;

bool bfs(int start){
queue <int> q;
q.push(start);
visit[start]=true;
while (!q.empty()){
int v=q.front();
q.pop();
for (int &u:adList[v]){
if (visit[u]) {
if (p[u]==p[v]) return false;
continue;
}
visit[u]=true;
p[u]=(p[v]+1)%2;
q.push(u);
}
}
return true;
}
int main(){
fastio;
cin>>n>>m;
adList.assign(n,vector<int>());
visit.assign(n,0);
p.assign(n,-1);
int x,y;
for (int i=0;i<m;++i){
cin>>x>>y;x--;y--;
adList[x].push_back(y);
adList[y].push_back(x);
}
for (int start=0;start<n;++start){
if (visit[start]) continue;
p[start]=0;
visit[start]=true;
if (!bfs(start)){
cout<<"IMPOSSIBLE\n";
return 0;
}
}
for (int i=0;i<n;++i) {
if (i!=0) cout<<' ';
cout<<p[i]+1;
}
cout<<'\n';
return 0;
}
44 changes: 44 additions & 0 deletions problem-of-the-day/day12/Connect Points.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// We will use Prim's Algorithm (is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph.)
// and min-heap data structure(is a tree-like data structure that always stores the minimum
// valued element (edge weight here) at the root) to solve this problem.

// In this apporach we will pick any random node then select the lowest weight node that connect a node present in MST to a node
// that is not present in MST and will keep it in min-heap and we use one boolean vector to record which node is already present
// in the MST

class Solution {
public:
int minCostConnectPoints(vector<vector<int>>& points) {
int n = points.size();
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> heap; //creating heap to store minimum weight
vector<bool> arr(n);
heap.push({ 0, 0 });
int mincost = 0;
int remaning = 0;
while (remaning < n) {
pair<int, int> topElement = heap.top();
heap.pop();

int weight = topElement.first;
int currNode = topElement.second;
if (arr[currNode]) {
continue;
}

arr[currNode] = true;
mincost += weight;
remaning++;

for (int nextNode = 0; nextNode < n; ++nextNode) {
if (!arr[nextNode]) {
int nextWeight = abs(points[currNode][0] - points[nextNode][0]) +
abs(points[currNode][1] - points[nextNode][1]);

heap.push({ nextWeight, nextNode });
}
}
}

return mincost;
}
};
19 changes: 19 additions & 0 deletions problem-of-the-day/day12/Find Champion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Count the indegree of each node
// The node with zero in-degree is the strongest and winner. If there are multiple zero in-degree nodes then return -1

int findChampion(int n, vector<vector<int>> &edges)
{
int ans = -1, count = 0;
vector<int> indegree(n, 0);
for (auto e : edges)
indegree[e[1]]++;
for (int i = 0; i < n; ++i)
{
if (indegree[i] == 0)
{
count++;
ans = i;
}
}
return count > 1 ? -1 : ans;
}
65 changes: 65 additions & 0 deletions problem-of-the-day/day12/High Score.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxN = 2501;
const int maxM = 5001;
const ll INF = 0x3f3f3f3f3f3f3f3f;

struct Edge {
int a, b; ll c;
} edges[maxM];

int N, M;
ll dp[maxN];
bool vis[maxN], visR[maxN];
vector<int> G[maxN], GR[maxN];

void dfs(int u){
vis[u] = true;
for(int v : G[u])
if(!vis[v])
dfs(v);
}

void dfsR(int u){
visR[u] = true;
for(int v : GR[u])
if(!visR[v])
dfsR(v);
}

int main(){
scanf("%d %d", &N, &M);
for(int i = 0, a, b; i < M; i++){
ll c;
scanf("%d %d %lld", &a, &b, &c);
edges[i] = {a, b, -c};
G[a].push_back(b);
GR[b].push_back(a);
}
dfs(1); dfsR(N);

fill(dp+2, dp+N+1, INF);
bool improvement = true;
for(int iter = 0; iter < N && improvement; iter++){
improvement = false;
for(int i = 0; i < M; i++){
int u = edges[i].a;
int v = edges[i].b;
ll w = edges[i].c;

if(dp[v] > dp[u]+w){
dp[v] = dp[u]+w;
improvement = true;

if(iter == N-1 && vis[v] && visR[v]){
printf("-1\n");
return 0;
}
}
}
}

printf("%lld\n", -dp[N]);
}
37 changes: 37 additions & 0 deletions problem-of-the-day/day12/Strongly Connected City.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// For this we already might need some kind of algorithm to solve it, though for the constraints given you might use almost any approach.
// If you just have a directed graph then in order to check whether you can reach all nodes from all other nodes you can do different things —
// you can dfs/bfs from all nodes, two dfs/bfs from the single node to check that every other node has both paths to and from that node,
// or you can employ different "min distance between all pairs of nodes" algorithm.
// All of these seem to work fine for this problem.

// But we shouldn't ignore here that the input graph has a special configuration — it's a grid of vertical and horizontal directed lines.
// For this graph all you need is to check whether four outer streets form a cycle or not.
// If they form a cycle then the answer is "YES", otherwise "NO".

// Here is an explanation for such short solution:

// If outer streets (top, bottom, rightmost and leftmost) form a cycle (clockwise or counter-clockwise), then
// by following that cycle, one can reach any junction on that cycle from any other junction on the same cycle.
// moreover, independent of the directions of inner streets you can also reach all "inner" junctions from any "outer" junction.
// Therefore, if the outer streets form a cycle, then the answer is "YES".
// If outer streets do not form a cycle, then

// there is at least one "corner" junction, for which both horizontal and vertical streets have directions towards that junction
// (otherwise, there it would be a cycle). Since one can not move out from such "corner" junction to anywhere else, the answer is "NO".


#include <iostream>
#include <string>

using namespace std;

int main() {
int x; cin >> x >> x;

string s1; cin >> s1;
string s2; cin >> s2;

string s3 = { s1.front() , s1.back() , s2.front() , s2.back() };

cout << (s3 == "<>v^" || s3 == "><^v" ? "YES" : "NO");
}

0 comments on commit 5ba2c75

Please sign in to comment.