diff --git a/problem-of-the-day/day12/Building Teams.cpp b/problem-of-the-day/day12/Building Teams.cpp new file mode 100644 index 0000000..d3e75d0 --- /dev/null +++ b/problem-of-the-day/day12/Building Teams.cpp @@ -0,0 +1,59 @@ +# include +# 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 > adList; +vector visit; +vector p; + +bool bfs(int start){ + queue 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()); + visit.assign(n,0); + p.assign(n,-1); + int x,y; + for (int i=0;i>x>>y;x--;y--; + adList[x].push_back(y); + adList[y].push_back(x); + } + for (int start=0;start>& points) { + int n = points.size(); + priority_queue, vector>, greater>> heap; //creating heap to store minimum weight + vector arr(n); + heap.push({ 0, 0 }); + int mincost = 0; + int remaning = 0; + while (remaning < n) { + pair 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; + } +}; \ No newline at end of file diff --git a/problem-of-the-day/day12/Find Champion.cpp b/problem-of-the-day/day12/Find Champion.cpp new file mode 100644 index 0000000..1fc4ec4 --- /dev/null +++ b/problem-of-the-day/day12/Find Champion.cpp @@ -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> &edges) +{ + int ans = -1, count = 0; + vector 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; +} \ No newline at end of file diff --git a/problem-of-the-day/day12/High Score.cpp b/problem-of-the-day/day12/High Score.cpp new file mode 100644 index 0000000..8f71f17 --- /dev/null +++ b/problem-of-the-day/day12/High Score.cpp @@ -0,0 +1,65 @@ +#include + +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 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]); +} \ No newline at end of file diff --git a/problem-of-the-day/day12/Strongly Connected City.cpp b/problem-of-the-day/day12/Strongly Connected City.cpp new file mode 100644 index 0000000..40fd98b --- /dev/null +++ b/problem-of-the-day/day12/Strongly Connected City.cpp @@ -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 +#include + +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"); +} \ No newline at end of file