├── 111. Minimum Depth of Binary Tree-LeetCode ├── 112. Path Sum- LeetCode ├── 113. Path Sum II-LeetCode ├── 206. Reverse Linked List-LeetCode ├── 216. Combination Sum III-LeetCode ├── 230. Kth Smallest Element in a BST-LeetCode ├── 257. Binary Tree Paths-LeetCode ├── 357. Count Numbers with Unique Digits-LeetCode ├── 404. Sum of Left Leaves-LeetCode ├── 46. Permutations-LeetCode ├── 494. Target Sum-LeetCode ├── 513. Find Bottom Left Tree Value-LeetCode ├── 515. Find Largest Value in Each Tree Row-LeetCode ├── 669. Trim a Binary Search Tree-LeetCode ├── 671. Second Minimum Node In a Binary Tree-LeetCode ├── 700. Search in a Binary Search Tree-LeetCode ├── 78. Subsets-LeetCode ├── 784. Letter Case Permutation-LeetCode ├── 83. Remove Duplicates from Sorted List-LeetCode ├── 872. Leaf-Similar Trees-LeetCode ├── 90. Subsets II-LeetCode ├── Codeforces 404C - Restore Graph .cpp ├── Codeforces 1130C - Connect.cpp ├── Codeforces 13C - Sequence.cpp ├── Codeforces 466E - Information Graph.cpp ├── Codeforces 542E - Playing on Graph.cpp ├── Codeforces 586D - Phillip and Trains.cpp ├── Codeforces 782E - E. Underground Lab.cpp ├── Codeforces : 1082D - Maximum Diameter Graph.cpp ├── Codeforces : 558C - Amr and Chemistry.cpp ├── Codeforces : D - Chemical table.cpp ├── Codeforces D - Mouse Hunt.cpp ├── Codeforces D. Fix a Tree.cpp ├── Codeforces D. Secret Passwords.cpp ├── Codeforces-129B - Students and Shoelaces.cpp ├── Codeforces-20C - Dijkstra.cpp ├── Codeforces-246D - Colorful Graph.cpp ├── Codeforces-277A - Learning Languages.cpp ├── Codeforces-292B - Network Topology.cpp ├── Codeforces-330B - Road Construction.cpp ├── Codeforces-369C - Valera and Elections.cpp ├── Codeforces-402C - Searching for Graph.cpp ├── Codeforces-417C - Football.cpp ├── Codeforces-427C - Checkposts.cpp ├── Codeforces-437C - The Child and Toy.cpp ├── Codeforces-463D - Gargari and Permutations.cpp ├── Codeforces-500B - New Year Permutation.cpp ├── Codeforces-501C - Misha and Forest.cpp ├── Codeforces-505B - Mr. Kitayuta's Colorful Graph.cpp ├── Codeforces-510B - Fox And Two Dots.cpp ├── Codeforces-546C - Soldier and Cards.cpp ├── Codeforces-574B - Bear and Three Musketeers.cpp ├── Codeforces-580C - Kefa and Park.cpp ├── Codeforces-601A - The Two Routes.cpp ├── Codeforces-763A - Timofey and a tree.cpp ├── Codeforces-A - Hongcow Builds A Nation.cpp ├── Codeforces-A - NP-Hard Problem.cpp ├── Codeforces-B - DZY Loves Chemistry.cpp ├── Codeforces-B - Greg and Graph.cpp ├── Codeforces: 723D - Lakes in Berland.cpp ├── HackerEarth Permutation Swaps.cpp ├── HackerEarth- Bishu and his Girlfriend.cpp ├── HackerEarth- Chocolate Journey.cpp ├── HackerEarth- DAY 3 - Engineering Entrance Exam.cpp ├── HackerEarth- Find the Flow.cpp ├── HackerEarth- Friendless Dr. Sheldon Cooper.cpp ├── HackerEarth- Mancunian Goes To The Derby.cpp ├── HackerEarth- Monk and the Islands.cpp ├── HackerEarth- Mr. President.cpp ├── HackerEarth- New Friends.cpp ├── HackerEarth- Oliver and the battle.cpp ├── HackerEarth- Permutation.cpp ├── HackerEarth- Quantitative coefficient.cpp ├── HackerEarth- Rhezo and Destructive Mind.cpp ├── HackerEarth- Rhezo and HackerEarth.cpp ├── HackerEarth- Sg against crime.cpp ├── HackerEarth- Space smugglers.cpp ├── HackerEarth- Travel diaries.cpp ├── HackerEarth-Articulation Points and Bridges.cpp ├── HackerEarth-Breadth First Search.cpp ├── HackerEarth-Depth First Search.cpp ├── HackerEarth-Dhoom 4.cpp ├── HackerEarth-Flood-fill Algorithm.cpp ├── HackerEarth-Happy Vertices.cpp ├── HackerEarth-Minimum Spanning Tree.cpp ├── HackerEarth-Monk and Graph G.cpp ├── HackerEarth-Monk and Hops.cpp ├── HackerEarth-Shortest Path Algorithms.cpp ├── HackerEarth-Smart travel agent.cpp ├── HackerEarth-So NP.cpp ├── HackerEarth-Strongly Connected Components ├── HackerEarth-The Parking Slot.cpp ├── HackerEarth-Topological Sort.cpp ├── HackerEarth-Trustworthy network.cpp ├── Learning Graph -HackerEarth ├── LeetCode 104. Maximum Depth of Binary Tree ├── Light oj 1009 - Back to Underworld.cpp ├── Light oj 1012 - Guilty Prince.cpp ├── LightOj 1049 - One Way Roads.cpp ├── LightOj 1141 - Number Transformation.cpp ├── LightOj- 1066 - Gathering Food.cpp ├── LightOj- 1094 - Farthest Nodes in a Tree.cpp ├── LightOj- 1111 Best Picnic Ever.cpp ├── LightOj- 1257 - Farthest Nodes in a Tree (II).cpp ├── LightOj- 1263 - Equalizing Money.cpp ├── Monk at the Graph Factory - HackerEarth ├── Monk in the real estate -HackerEarth ├── SPOJ BENEFACT - The Benefactor.cpp ├── SPOJ SHPATH - The Shortest Path.cpp ├── SPOJ-BUGLIFE - A Bug’s Life.cpp ├── SPOJ-EC_P - Critical Edges.cpp ├── SPOJ-MTOTALF - Total Flow.cpp ├── SPOJ-POTHOLE - Potholers.cpp ├── SPOJ-PT07Y - Is it a tree.cpp ├── SPOJ-PT07Z - Longest path in a tree.cpp ├── SPOJ-TRAFFICN - Traffic Network.cpp ├── SPOJ-TRVCOST - Travelling cost.cpp ├── codeforces-448C - Painting Fence.cpp ├── e-Olymp-Cities and roads.cpp ├── e-Olymp-Connectivity.cpp ├── e-Olymp-Depth Search.cpp ├── e-Olymp-Get a tree.cpp ├── e-Olymp-Is there a cycle.cpp └── e-Olymp-Traffic lights.cpp /111. Minimum Depth of Binary Tree-LeetCode: -------------------------------------------------------------------------------- 1 | // Just Use Traversing By Recursively 2 | 3 | class Solution { 4 | public: 5 | int ans=INT_MAX; 6 | void max(TreeNode* root,int lev) 7 | { 8 | if(root->left==NULL&&root->right==NULL) 9 | { 10 | if(levleft!=NULL) 16 | { 17 | max(root->left,lev+1); 18 | } 19 | if(root->right!=NULL) 20 | { 21 | max(root->right,lev+1); 22 | } 23 | } 24 | int minDepth(TreeNode* root) { 25 | if(root==NULL) 26 | { 27 | return 0; 28 | } 29 | max(root,0); 30 | return ans+1; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /112. Path Sum- LeetCode: -------------------------------------------------------------------------------- 1 | // Just One Kind Up Recursion 2 | 3 | class Solution { 4 | public: 5 | bool hasPathSum(TreeNode* root, int sum) { 6 | if (root == NULL) { return false; } 7 | sum -= root->val; 8 | 9 | //We found a leaf w/ correct sum... 10 | if (root->left == NULL && root->right == NULL && sum == 0) { return true; } 11 | 12 | //Try L an R 13 | if (hasPathSum(root->left, sum)) { return true; } 14 | if (hasPathSum(root->right, sum)) { return true; } 15 | 16 | return false; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /113. Path Sum II-LeetCode: -------------------------------------------------------------------------------- 1 | // Just Use of Bracktracking and vector 2 | 3 | class Solution { 4 | public: 5 | 6 | vector> pathSum(TreeNode* root, int sum) { 7 | 8 | vector> aResult; 9 | if (root == NULL) { 10 | return aResult; 11 | } 12 | 13 | vector aRunningList; 14 | pathSumHelper(aResult, aRunningList, root, sum); 15 | 16 | return aResult; 17 | 18 | } 19 | 20 | void pathSumHelper(vector> &pResult, vector &pRunningList, TreeNode* root, int sum) { 21 | 22 | if (root == NULL) { 23 | return; 24 | } 25 | 26 | pRunningList.push_back(root->val); 27 | 28 | sum -= root->val; 29 | 30 | if (sum == 0 && root->left == NULL && root->right == NULL) { 31 | pResult.push_back(pRunningList); 32 | } 33 | 34 | pathSumHelper(pResult, pRunningList, root->left, sum); 35 | pathSumHelper(pResult, pRunningList, root->right, sum); 36 | 37 | pRunningList.pop_back(); 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /206. Reverse Linked List-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | public: 5 | ListNode* reverseList(ListNode* head) { 6 | ListNode* prev=NULL; 7 | ListNode* start=head; 8 | ListNode* node=head; 9 | while(node!=NULL) 10 | { 11 | start=node->next; 12 | node->next=prev; 13 | prev=node; 14 | node=start; 15 | } 16 | return prev; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /216. Combination Sum III-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | vector> ans; 5 | void tutil(vector&curr,int k,int target,int count,int index) 6 | { 7 | if(target==0 && count==k) 8 | { 9 | ans.push_back(curr); 10 | } 11 | if(target<0) 12 | return; 13 | if(count>k) 14 | return; 15 | 16 | for(int i=index;i<=9;i++) 17 | { 18 | curr.push_back(i); 19 | tutil(curr,k,target-i,count+1,i+1); 20 | curr.pop_back(); 21 | } 22 | 23 | } 24 | vector> combinationSum3(int k, int n) { 25 | vectorcurr; 26 | tutil(curr,k,n,0,1); 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /230. Kth Smallest Element in a BST-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | public: 5 | vectorv; 6 | void preorder(TreeNode* root) 7 | { 8 | v.push_back(root->val); 9 | if(root->left!=NULL) 10 | { 11 | preorder(root->left); 12 | } 13 | 14 | if(root->right!=NULL) 15 | { 16 | preorder(root->right); 17 | } 18 | } 19 | 20 | int kthSmallest(TreeNode* root, int k) { 21 | preorder(root); 22 | sort(v.begin(),v.end()); 23 | return v[k-1]; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /257. Binary Tree Paths-LeetCode: -------------------------------------------------------------------------------- 1 | // Just Use Simple Backtracking 2 | 3 | class Solution { 4 | private: 5 | vector res; 6 | vector stk; 7 | vectorv; 8 | 9 | void traverse(TreeNode* root) { 10 | if (root == nullptr) { 11 | return; 12 | } 13 | 14 | if (root->left == nullptr && root->right == nullptr) { 15 | string path; 16 | for (auto& s : stk) { 17 | path += s; 18 | } 19 | path += to_string(root->val); 20 | res.push_back(path); 21 | return; 22 | } 23 | 24 | stk.push_back(to_string(root->val) + "->"); 25 | v.push_back(root->val); 26 | traverse(root->left); 27 | traverse(root->right); 28 | stk.pop_back(); 29 | v.pop_back(); 30 | } 31 | public: 32 | vector binaryTreePaths(TreeNode* root) { 33 | traverse(root); 34 | return res; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /357. Count Numbers with Unique Digits-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | // f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0].Just Use Basics Of combinatorics. 3 | class Solution { 4 | public: 5 | int countNumbersWithUniqueDigits(int n) { 6 | int n2=91,head=81,x=9; 7 | if(n==0) 8 | return 1; 9 | else if(n==1) 10 | return 10; 11 | for(int i=1;i<=n-2;i++) 12 | { 13 | head*=(x-i); 14 | n2+=head; 15 | } 16 | return n2; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /404. Sum of Left Leaves-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | public: 5 | int sum=0; 6 | void pre(TreeNode* root,int n) 7 | { 8 | if(n==0) 9 | { 10 | if(root->left==NULL&&root->right==NULL){ 11 | sum=sum+(root->val); 12 | } 13 | } 14 | if(root->left!=NULL) 15 | { 16 | 17 | pre(root->left,0); 18 | } 19 | if(root->right!=NULL) 20 | { 21 | pre(root->right,1); 22 | } 23 | } 24 | int sumOfLeftLeaves(TreeNode* root) { 25 | if(root==NULL) 26 | return 0; 27 | if(root->left==NULL&&root->right==NULL) 28 | return 0; 29 | 30 | int n=root->val; 31 | pre(root,0); 32 | return sum; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /46. Permutations-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | 5 | vector>ans; 6 | void permute(vector&curr,vector&nums,int index,int n) 7 | { 8 | if(index==n) 9 | { 10 | for(int j=0;j> permute(vector& nums) { 29 | vectorcurr; 30 | permute(curr,nums,0,nums.size()); 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /494. Target Sum-LeetCode: -------------------------------------------------------------------------------- 1 | // Normal Recursive 2 | 3 | class Solution { 4 | public: 5 | int count=0; 6 | void countagain(vector&nums,int S,int i,int sum) 7 | { 8 | if(i==nums.size()) 9 | { 10 | if(sum==S){ 11 | count++; 12 | } 13 | return; 14 | } 15 | 16 | countagain(nums,S,i+1,sum+nums[i]); 17 | countagain(nums,S,i+1,sum-nums[i]); 18 | } 19 | int findTargetSumWays(vector& nums, int S) { 20 | countagain(nums,S,0,0); 21 | return count; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /513. Find Bottom Left Tree Value-LeetCode: -------------------------------------------------------------------------------- 1 | // 2 | 3 | class Solution { 4 | public: 5 | int ans,m; 6 | bool lf=false,rt=false; 7 | void pre(TreeNode* root,int val) 8 | { 9 | if(mval; 13 | } 14 | if(root->left!=NULL) 15 | { 16 | lf=true; 17 | pre(root->left,val+1); 18 | } 19 | if(root->right!=NULL) 20 | { 21 | pre(root->right,val+1); 22 | } 23 | } 24 | int findBottomLeftValue(TreeNode* root) { 25 | if(root->left==NULL&&root->right==NULL) 26 | { 27 | return root->val; 28 | } 29 | pre(root,0); 30 | 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /515. Find Largest Value in Each Tree Row-LeetCode: -------------------------------------------------------------------------------- 1 | // Just Use map Conatiner To Hold The Value Of Every Level 2 | 3 | class Solution { 4 | public: 5 | int mx=0; 6 | map>m; 7 | void pre(TreeNode* root,int lev) 8 | { 9 | mx=max(lev,mx); 10 | m[lev].push_back(root->val); 11 | if(root->left!=NULL) 12 | { 13 | pre(root->left,lev+1); 14 | } 15 | if(root->right!=NULL) 16 | { 17 | pre(root->right,lev+1); 18 | } 19 | } 20 | vector largestValues(TreeNode* root) { 21 | vectorp; 22 | if(root==NULL) 23 | { 24 | return p; 25 | } 26 | 27 | int ans=0; 28 | pre(root,0); 29 | for(int i=0;i<=mx;i++) 30 | { 31 | ans=INT_MIN; 32 | for(int j=0;j R) return trimBST(root.left, L, R); 7 | if (root.val < L) return trimBST(root.right, L, R); 8 | 9 | root.left = trimBST(root.left, L, R); 10 | root.right = trimBST(root.right, L, R); 11 | return root; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /671. Second Minimum Node In a Binary Tree-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | public: 5 | setv; 6 | void preorder(TreeNode* root) 7 | { 8 | v.insert(root->val); 9 | if(root->left!=NULL) 10 | { 11 | preorder(root->left); 12 | } 13 | 14 | if(root->right!=NULL) 15 | { 16 | preorder(root->right); 17 | } 18 | } 19 | 20 | int findSecondMinimumValue(TreeNode* root) { 21 | preorder(root); 22 | if(v.size()<2) 23 | { 24 | return -1; 25 | } 26 | set::iterator iter=v.begin(); 27 | iter++; 28 | return (*iter); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /700. Search in a Binary Search Tree-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | public: 5 | TreeNode* node=NULL; 6 | 7 | void order(TreeNode *root,int value) 8 | { 9 | if(root->val==value) 10 | { 11 | node=root; 12 | return; 13 | } 14 | if(root->right!=NULL) 15 | { 16 | order(root->right,value); 17 | } 18 | if(root->left!=NULL) 19 | { 20 | order(root->left,value); 21 | } 22 | } 23 | TreeNode* searchBST(TreeNode* root, int val) { 24 | order(root,val); 25 | return node; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /78. Subsets-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | public: 5 | int n; 6 | vectorsus; 7 | void push(vector>& sums,int A[],int i,vector& nums) 8 | { 9 | for(int j=0;j>& sums,int A[],int i,int n,vector& nums) 20 | { 21 | if(i==n) 22 | { 23 | push(sums,A,n,nums); 24 | return; 25 | } 26 | A[i]=0; 27 | solve(sums,A,i+1,n,nums); 28 | A[i]=1; 29 | solve(sums,A,i+1,n,nums); 30 | } 31 | vector> subsets(vector& nums) { 32 | vector>sums; 33 | n=nums.size(); 34 | int A[n]={0}; 35 | solve(sums,A,0,n,nums); 36 | return sums; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /784. Letter Case Permutation-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | public: 5 | void permu(vector&res,string temp,string s,int start) 6 | { 7 | if(start==s.size()) 8 | { 9 | res.push_back(temp); 10 | return; 11 | } 12 | if(s[start]>='0'&&s[start]<='9') 13 | { 14 | temp=temp+s[start]; 15 | permu(res,temp,s,start+1); 16 | } 17 | else 18 | { 19 | char c=(tolower(s[start])); 20 | permu(res,temp+c,s,start+1); 21 | c=(toupper(s[start])); 22 | permu(res,temp+c,s,start+1); 23 | } 24 | 25 | } 26 | vector letterCasePermutation(string S) { 27 | 28 | vectorres; 29 | string temp=""; 30 | permu(res,temp,S,0); 31 | return res; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /83. Remove Duplicates from Sorted List-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | public: 5 | ListNode* deleteDuplicates(ListNode* head) { 6 | ListNode* node=head; 7 | int prev=-1; 8 | ListNode *pr; 9 | while(node!=NULL) 10 | { 11 | if(node->val!=prev) 12 | { 13 | prev=node->val; 14 | pr=node; 15 | } 16 | else 17 | { 18 | pr->next=node->next; 19 | } 20 | node=node->next; 21 | } 22 | return head; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /872. Leaf-Similar Trees-LeetCode: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | class Solution { 5 | public: 6 | vectorv1; 7 | vectorv2; 8 | void inder(TreeNode* root) 9 | { 10 | if(root->left==NULL&&root->right==NULL){ 11 | v2.push_back(root->val); 12 | } 13 | if(root->left!=NULL) 14 | { 15 | inder(root->left); 16 | } 17 | if(root->right!=NULL) 18 | { 19 | inder(root->right); 20 | } 21 | } 22 | void order(TreeNode* root) 23 | { 24 | if(root->left==NULL&&root->right==NULL){ 25 | v1.push_back(root->val); 26 | } 27 | if(root->left!=NULL) 28 | { 29 | order(root->left); 30 | } 31 | if(root->right!=NULL) 32 | { 33 | order(root->right); 34 | } 35 | } 36 | bool leafSimilar(TreeNode* root1, TreeNode* root2) { 37 | order(root1); 38 | inder(root2); 39 | 40 | for(int i=0;isus; 8 | void push(vector>& sums,int A[],int i,vector& nums) 9 | { 10 | for(int j=0;j>::iterator it=find(sums.begin(),sums.end(),sus); 18 | if(it==sums.end()){ 19 | sums.push_back(sus); 20 | } 21 | 22 | sus.clear(); 23 | } 24 | void solve(vector>& sums,int A[],int i,int n,vector& nums) 25 | { 26 | if(i==n) 27 | { 28 | push(sums,A,n,nums); 29 | return; 30 | } 31 | A[i]=0; 32 | solve(sums,A,i+1,n,nums); 33 | A[i]=1; 34 | solve(sums,A,i+1,n,nums); 35 | } 36 | vector> subsetsWithDup(vector& nums) { 37 | vector>sums; 38 | n=nums.size(); 39 | int A[n]={0}; 40 | sort(nums.begin(),nums.end()); 41 | solve(sums,A,0,n,nums); 42 | return sums; 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /Codeforces 404C - Restore Graph .cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | /// Codeforces 404C - Restore Graph 4 | /// Greedy + graph 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | typedef long long ll; 11 | 12 | const int mx=1e5+1; 13 | 14 | queue a[mx]; 15 | vector> ans; 16 | int n,k,t; 17 | 18 | 19 | int bfs() 20 | { 21 | queue> q; 22 | q.push( make_pair(a[0].front(),0) ); 23 | while(!q.empty()) 24 | { 25 | int cur = q.front().first; 26 | int step = q.front().second; 27 | q.pop(); 28 | int degree; 29 | if(step==0) degree=k; 30 | else degree=k-1; 31 | 32 | while(!a[step+1].empty()&°ree) 33 | { 34 | int tt = a[step+1].front(); 35 | a[step+1].pop(); 36 | ans.push_back( make_pair( cur, tt) ); 37 | q.push( make_pair(tt,step+1) ); 38 | degree--; 39 | } 40 | } 41 | return 0; 42 | } 43 | 44 | 45 | int main() 46 | { 47 | 48 | scanf("%d%d",&n,&k); 49 | for(int i=1;i<=n;i++) 50 | { 51 | scanf("%d",&t); 52 | a[t].push(i); 53 | } 54 | 55 | if(a[0].size()!=1) 56 | { 57 | printf("-1\n"); 58 | return 0; 59 | } 60 | 61 | if(a[1].size()>k) 62 | { 63 | printf("-1\n"); 64 | return 0; 65 | } 66 | 67 | for(int i=1;i (ll)a[i].size()*(k-1) )//need to use long long 71 | { 72 | printf("-1\n"); 73 | return 0; 74 | } 75 | } 76 | 77 | bfs(); 78 | int ansl =ans.size(); 79 | printf("%d\n",ansl); 80 | for(int i=0;i 10 | 11 | using namespace std; 12 | 13 | int nm; 14 | 15 | int vis[51][51]; 16 | char grid[51][51]; 17 | 18 | /// up - down - right - left 19 | int dx[]={0,0,1,-1}; 20 | int dy[]={1,-1,0,0}; 21 | 22 | 23 | void BFS(int src_x,int src_y,int val) 24 | { 25 | queue >q; 26 | vis[src_x][src_y]=val; /// consider it as vampire and color it as BLACK 27 | q.push(make_pair(src_x,src_y)); 28 | 29 | while(!q.empty()) 30 | { 31 | int x=q.front().first; 32 | int y=q.front().second; 33 | q.pop(); 34 | 35 | for(int i=0;i<4;i++) 36 | { 37 | int xx=x+dx[i]; 38 | int yy=y+dy[i]; 39 | if(xx>=0 && yy>=0 && xx>nm; 58 | int src_x,src_y,dst_x,dst_y; 59 | cin>>src_x>>src_y>>dst_x>>dst_y; 60 | src_x--;src_y--;dst_x--;dst_y--; 61 | for(int i=0;i>grid[i][j]; 66 | } 67 | } 68 | 69 | BFS(src_x,src_y,1); 70 | if(vis[dst_x][dst_y]==1) 71 | { 72 | cout<<"0"< 6 | using namespace std; 7 | #define int long long 8 | int n,res; 9 | priority_queue pq; 10 | main() 11 | { 12 | cin >> n; 13 | for(int i=1;i<=n;++i) 14 | { 15 | int x; cin >> x; 16 | if(!pq.empty() && pq.top() > x) 17 | { 18 | res += pq.top() - x; 19 | pq.pop(); 20 | pq.push(x); 21 | } 22 | pq.push(x); 23 | } 24 | cout << res; 25 | return 0; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Codeforces 466E - Information Graph.cpp: -------------------------------------------------------------------------------- 1 | 2 | /// Codeforces 466E - Information Graph 3 | /// Nice one ,dsu 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | typedef long long ll; 10 | 11 | const int mx=100100; 12 | 13 | int father[mx]; 14 | int boss[mx]; 15 | 16 | vectorsign; 17 | vectorsign_fa; 18 | 19 | int find_fat(int x) 20 | { 21 | if(x==father[x]) return x; 22 | return father[x]=find_fat(father[x]); 23 | } 24 | 25 | 26 | int main() 27 | 28 | { 29 | int n,m; 30 | cin>>n>>m; 31 | for(int i=0;i>typ; 37 | if(typ==1) 38 | { 39 | cin>>a>>b; 40 | boss[a]=b; 41 | father[a]=b; 42 | } 43 | 44 | else if(typ==2) 45 | { 46 | cin>>a; 47 | sign.push_back(a); 48 | sign_fa.push_back(find_fat(a)); 49 | } 50 | 51 | else if(typ==3) 52 | { 53 | cin>>a>>b; 54 | b--; 55 | int frst=sign[b]; 56 | int secnd=sign_fa[b]; 57 | bool flag=false; 58 | 59 | while(true) 60 | { 61 | /// a man from cycle hold this 62 | if(frst==a) {flag=true;break;} 63 | /// no sign 64 | if(frst==secnd) break; 65 | frst=boss[frst]; 66 | } 67 | 68 | if(flag) puts("YES"); 69 | else puts("NO"); 70 | } 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Codeforces 542E - Playing on Graph.cpp: -------------------------------------------------------------------------------- 1 | 2 | /// Codeforces 542E - Playing on Graph 3 | /// Only DFS + BFS 4 | /// Nice one ! 5 | 6 | /// When two points are not connected ,then one will be deleted 7 | /// We have to find the maximum chain's length 8 | /// So, check the odd cycle which reflects the impossible 9 | /// So, find the chain length of each connected component 10 | 11 | #include 12 | 13 | typedef long long ll; 14 | 15 | using namespace std; 16 | 17 | const int maxx=100003; 18 | 19 | vectoradj[maxx]; 20 | 21 | bool visited[maxx]; 22 | /// it will keep the chain length of a particular connected component i 23 | int max_chain_len[maxx]; 24 | int color[maxx]; 25 | /// it will take distance from particular node in a connected component 26 | int dist[maxx]; 27 | /// store the number of connected component in which the node i exists 28 | int num_of_cnnctd_cmpnnt[maxx]; 29 | 30 | int num=0; 31 | 32 | bool isBipartite( int v) 33 | { 34 | num_of_cnnctd_cmpnnt[v]=num; 35 | 36 | for (int u : adj[v]) { 37 | 38 | // if vertex u is not explored before 39 | if (visited[u] == false) { 40 | 41 | // mark present vertic as visited 42 | visited[u] = true; 43 | 44 | // mark its color opposite to its parent 45 | color[u] = !color[v]; 46 | 47 | // if the subtree rooted at vertex v is not bipartite 48 | if (!isBipartite( u)) 49 | return false; 50 | } 51 | 52 | // if two adjacent are colored with same color then 53 | // the graph is not bipartite 54 | else if (color[u] == color[v]) 55 | return false; 56 | } 57 | return true; 58 | } 59 | 60 | int main() 61 | { 62 | queueQ; 63 | int n,m,a,b; 64 | cin>>n>>m; 65 | 66 | while(m--) 67 | { 68 | cin>>a>>b; 69 | adj[a].push_back(b); 70 | adj[b].push_back(a); 71 | } 72 | 73 | memset(visited,false,sizeof(visited)); 74 | bool odd_cycle=false; 75 | for(int i=1;i<=n;i++) 76 | { 77 | if(visited[i]==false) 78 | { 79 | num++; 80 | if(!isBipartite(i)) odd_cycle=true; 81 | } 82 | } 83 | 84 | if(odd_cycle) {cout<<"-1"< 6 | 7 | using namespace std; 8 | 9 | int n,k,srcx,srcy; 10 | char grid[10][120]; 11 | bool flg; 12 | int vis[10][120]; 13 | 14 | bool judge(int x,int y){ 15 | return (x >= 0 && x < 3 && (grid[x][y] == '.' || grid[x][y] == 's')); 16 | } 17 | 18 | void man_go(int x,int y) 19 | { 20 | vis[x][y] = 1; 21 | if (y >= n - 1){ 22 | flg = true; 23 | return; 24 | } 25 | if (flg) return; 26 | /// he will be caught 27 | if (!judge(x,y+1)) return; 28 | if (judge(x,y+1) && judge(x,y+2) && judge(x,y+3) && !vis[x][y+3]) { 29 | man_go(x,y+3); 30 | } 31 | if (judge(x+1,y+1) && judge(x+1,y+2) && judge(x+1,y+3) && !vis[x+1][y+3]) { 32 | man_go(x+1,y+3); 33 | } 34 | if (judge(x-1,y+1) && judge(x-1,y+2) && judge(x-1,y+3) && !vis[x-1][y+3]) { 35 | man_go(x-1,y+3); 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | int t; 42 | cin >> t; 43 | while(t--) 44 | { 45 | memset(vis,0,sizeof(vis)); 46 | flg = false; 47 | cin >> n >> k; 48 | for (int i = 0;i < 3;i++) 49 | { 50 | for (int j = 0;j < n;j++) 51 | { 52 | cin >> grid[i][j]; 53 | if (grid[i][j] == 's') srcx = i,srcy = j; 54 | } 55 | /// for extra memory of dfs 56 | for (int j = n;j < 115;j++) grid[i][j] = '.'; 57 | } 58 | man_go(srcx,srcy); 59 | if (flg) cout << "YES\n"; else cout << "NO\n"; 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Codeforces 782E - E. Underground Lab.cpp: -------------------------------------------------------------------------------- 1 | 2 | /// Codeforces 782E - E. Underground Lab 3 | /// DFS Problem 4 | /// Nice problem!! 5 | 6 | 7 | #include 8 | 9 | typedef long long ll; 10 | 11 | using namespace std; 12 | 13 | const int maxx=200002; 14 | 15 | vectoradj[maxx]; 16 | vectorans; 17 | int vis[maxx]; 18 | int n,m,k; 19 | 20 | void dfs(int u) 21 | { 22 | vis[u] = 1; 23 | ans.push_back(u); 24 | for(int i = 0; i < adj[u].size(); i++){ 25 | int v = adj[u][i]; 26 | if(vis[v]) continue; 27 | dfs(v); 28 | ans.push_back(u); 29 | } 30 | } 31 | 32 | int main() 33 | { 34 | 35 | int u,v; 36 | cin >> n >> m >> k; 37 | for(int i = 0; i < m; i++) 38 | { 39 | cin>>u>>v; 40 | adj[u].push_back(v); 41 | adj[v].push_back(u); 42 | } 43 | /// a dfs eulerian path where total there are 2*n entries 44 | /// starting and end node is the same for eulerian path 45 | dfs(1); 46 | /// it is for the size of part if we take eulerian path 47 | /// every starting node is another count for path , so total entries will be 2*n-1+k 48 | int stp = (2*n+k-1)/k; 49 | for(int i = 0; i < k; i++) 50 | { 51 | int beg = i*stp, ends = min((i+1)*stp, (int)ans.size()); 52 | if(ends <= beg) 53 | { 54 | printf("1 1\n"); 55 | continue; 56 | } 57 | 58 | printf("%d", ends-beg); 59 | for(int j = beg; j < ends; j++) 60 | { 61 | printf(" %d", ans[j]); 62 | } 63 | puts(""); 64 | } 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Codeforces : 1082D - Maximum Diameter Graph.cpp: -------------------------------------------------------------------------------- 1 | 2 | /// Codeforces : 1082D - Maximum Diameter Graph. 3 | /// Category : graph + implementation. 4 | 5 | 6 | /// It is known that if we want to make maximize the diameter of the graph 7 | /// Then it should be tree 8 | /// So, a tree has n-1 edges ,if total degrees can't fill up it 9 | /// Then it's impossible to make the graph . 10 | 11 | #include 12 | 13 | 14 | using namespace std; 15 | 16 | 17 | int main() 18 | 19 | { 20 | ios_base::sync_with_stdio(false); 21 | cin.tie(NULL); 22 | cout.tie(NULL); 23 | 24 | int n; 25 | cin>>n; 26 | int degree[n+1]; 27 | for(int i=1;i<=n;i++) cin>>degree[i]; 28 | 29 | vectorleaf; 30 | vectorother; 31 | 32 | int sum=0; 33 | 34 | for(int i=1;i<=n;i++) 35 | { 36 | /// these will be leaf nodes 37 | if(degree[i]==1) 38 | { 39 | leaf.push_back(i); 40 | } 41 | /// these will be other nodes 42 | else 43 | { 44 | other.push_back(i); 45 | } 46 | 47 | sum+=degree[i]; 48 | } 49 | 50 | if(sum/2<(n-1)) {cout<<"NO"< >ans; 53 | /// making linear tree 54 | for(int i=1;i<(int)other.size();i++) 55 | { 56 | ans.push_back(make_pair(other[i-1],other[i])); 57 | degree[other[i]]--; 58 | degree[other[i-1]]--; 59 | } 60 | 61 | if((int)leaf.size()==1) 62 | { 63 | ans.push_back(make_pair(leaf[0],other[0])); 64 | degree[leaf[0]]--; 65 | degree[other[0]]--; 66 | 67 | cout<<"YES "; 68 | cout<<(int)other.size()<1) 72 | { 73 | int pos=0; 74 | /// it is greedy to add two end of the tree to make diameter maximize 75 | ans.push_back({other[0],leaf[0]}); 76 | degree[other[0]]--; 77 | ans.push_back({other[other.size()-1],leaf[1]}); 78 | degree[other[other.size()-1]]--; 79 | ///two already gone 80 | for(int i=2;i<(int)leaf.size();i++) 81 | { 82 | while(!degree[other[pos]]) pos++; 83 | ans.push_back(make_pair(leaf[i],other[pos])); 84 | degree[other[pos]]--; 85 | } 86 | cout<<"YES "; 87 | /// diameter will be 2(two leaf nodes at the begin and the end) + other size -1 . 88 | cout<<(int)other.size()+1< 10 | 11 | using namespace std; 12 | 13 | typedef long long ll; 14 | 15 | const int mx= 2000001; 16 | 17 | int times[mx]; 18 | int ans[mx]; 19 | 20 | queue,int> >pq; 21 | 22 | int main() 23 | 24 | { 25 | ios_base::sync_with_stdio(false); 26 | cin.tie(NULL); 27 | cout.tie(NULL); 28 | 29 | int n; 30 | cin>>n; 31 | int arr[n]; 32 | 33 | for(int i=0;i>arr[i]; 36 | pq.push(make_pair(make_pair(arr[i],0),0)); 37 | } 38 | 39 | while(!pq.empty()) 40 | { 41 | int num=pq.front().first.first; 42 | int previous=pq.front().first.second; 43 | int steps=pq.front().second; 44 | pq.pop(); 45 | /// this num will be counted 46 | times[num]++; 47 | ans[num]+=steps; 48 | if(num*2<=200000 && num*2!=previous) pq.push(make_pair(make_pair(2*num,num),steps+1)); 49 | if(num/2>0 && num/2!=num && num/2!=previous) pq.push(make_pair(make_pair(num/2,num),steps+1)); 50 | } 51 | 52 | int res=INT_MAX; 53 | 54 | for(int i=1;i<=200000;i++) 55 | { 56 | /// every number touches this state (i) 57 | if(times[i]==n) 58 | { 59 | res=min(res,ans[i]); 60 | } 61 | } 62 | 63 | cout< 18 | 19 | 20 | using namespace std; 21 | 22 | const int mx=400005; 23 | 24 | vectoradj[mx]; 25 | bool vis[mx]; 26 | 27 | int connected_components; 28 | 29 | void dfs(int u) 30 | { 31 | connected_components++; 32 | int sz=adj[u].size(); 33 | for(int i=0;i>n>>m>>q; 54 | 55 | while(q--) 56 | { 57 | cin>>u>>v; 58 | adj[u].push_back(v+n); 59 | adj[v+n].push_back(u); 60 | } 61 | 62 | /// so, the ranges of edges are 1 to (n+m) 63 | 64 | int ans=0; 65 | memset(vis,false,sizeof(vis)); 66 | 67 | for(int i=1;i<=n+m;i++) 68 | { 69 | if(!vis[i]) ans++,dfs(i); 70 | } 71 | 72 | cout<<(ans-1)< 8 | 9 | typedef long long ll; 10 | 11 | using namespace std; 12 | 13 | const int maxx=200002; 14 | 15 | int father[maxx]; 16 | int next_sec[maxx]; 17 | int cost[maxx]; 18 | int bl[maxx]; 19 | 20 | int find_final_fath(int x) 21 | { 22 | return x == father[x] ? x : father[x] = find_final_fath(father[x]); 23 | } 24 | 25 | /// finding minimum cost one amongst a block 26 | int dfs(int fa,int x) 27 | { 28 | if(fa == x) return cost[fa]; 29 | return min(dfs(next_sec[fa], x), cost[fa]); 30 | } 31 | 32 | int main() 33 | { 34 | int n; 35 | cin>>n; 36 | for(int i = 1; i <= n; i++) {cin>>cost[i];father[i] = i;} 37 | 38 | for(int i = 1; i <= n; i++) cin>>next_sec[i]; 39 | 40 | for(int i = 1; i <= n; i++) 41 | { 42 | /// that means father of this block 43 | if(find_final_fath(next_sec[i])==find_final_fath(i)) 44 | { 45 | bl[i]=1; 46 | continue; 47 | } 48 | father[find_final_fath(next_sec[i])] = find_final_fath(i); 49 | } 50 | 51 | int ans=0; 52 | 53 | for(int i=1;i<=n;i++) 54 | { 55 | if(bl[i]) 56 | { 57 | ans+=dfs(next_sec[i],i); 58 | } 59 | } 60 | 61 | cout< 7 | 8 | typedef long long ll; 9 | 10 | const int maxx=1000003; 11 | 12 | using namespace std; 13 | 14 | int parent[maxx]; 15 | 16 | int find_parent(int u) 17 | { 18 | if(parent[u]==u) return u; 19 | int f=find_parent(parent[u]); 20 | parent[u]=f; 21 | return f; 22 | } 23 | 24 | int main() 25 | { 26 | 27 | int res=0; 28 | int root=-1,n; 29 | cin>>n; 30 | int pa[n+1]; 31 | for(int i=0;i<=n;++i) parent[i]=i; 32 | 33 | for(int i=1;i<=n;++i){ 34 | cin>>pa[i]; 35 | if(pa[i]==i) root=i; 36 | } 37 | 38 | for(int i=1;i<=n;i++) 39 | { 40 | /// root of a tree 41 | if(i==root) continue; 42 | 43 | int fc=find_parent(i); 44 | int fp=find_parent(pa[i]); 45 | 46 | /// legal ,as fc and fp are not connected 47 | if(fc!=fp) parent[fc]=fp; 48 | 49 | /// illegal ,so change 50 | /// make father of root as parent of i node 51 | else 52 | { 53 | res++; 54 | if(root==-1) root=i; 55 | parent[fc]=find_parent(root); 56 | pa[i]=parent[fc]; 57 | 58 | } 59 | 60 | } 61 | 62 | cout< 11 | 12 | typedef long long ll; 13 | 14 | /// prime number greater than 10^5 15 | const int maxx=1000003; 16 | 17 | using namespace std; 18 | 19 | vectoradj[maxx]; 20 | int vis[maxx]; 21 | 22 | void dfs(int u) 23 | { 24 | for(int i=0;i>n; 38 | string s; 39 | 40 | for(int i=1;i<=n;i++) 41 | { 42 | cin>>s; 43 | for(int j=0;j 3 | 4 | using namespace std; 5 | 6 | 7 | int main() 8 | 9 | { 10 | int n,m,u,v; 11 | cin>>n>>m; 12 | map< int , pair >edge; 13 | map< pair , bool> edge_vis; 14 | int degrees[n+1]; 15 | memset(degrees,0,sizeof(degrees)); 16 | int i=0; 17 | for(i=0;i>u>>v; 20 | edge[i]=make_pair(u,v); 21 | degrees[u]++; 22 | degrees[v]++; 23 | } 24 | int total_group=0; 25 | while(1) 26 | { 27 | vector< pair >v; 28 | bool group=false; 29 | for(int i=0;ip; 32 | p=edge[i]; 33 | if((degrees[p.first]==1||degrees[p.second]==1) &&edge_vis[p]==false) 34 | { 35 | v.push_back(p); // these edge(student) will be kick out from the class 36 | group=true; 37 | } 38 | } 39 | for(int j=0;j 2 | 3 | using namespace std; 4 | 5 | const int mx=1e5+1; 6 | const unsigned long long int md=1e18; 7 | typedef pair pii; 8 | vector< pair >adj[mx]; 9 | unsigned long long int dist1[mx]; 10 | bool vis[mx]; 11 | int parent[mx]; 12 | 13 | void djkstra(int source,unsigned long long int *dist) 14 | 15 | { 16 | priority_queue,greater >pq; 17 | pq.push(make_pair(0,source)); 18 | dist[source]=0; 19 | while(!pq.empty()) 20 | { 21 | int u=pq.top().second; 22 | pq.pop(); 23 | if(vis[u]==true) continue; 24 | 25 | vis[u]=false; 26 | for(int i=0;idist[u]+weight) 31 | { 32 | dist[v]=dist[u]+weight; 33 | pq.push(make_pair(dist[v],v)); 34 | parent[v]=u; 35 | } 36 | } 37 | } 38 | 39 | } 40 | 41 | 42 | int main() 43 | 44 | { 45 | int n,m,a,b; 46 | unsigned long long int w; 47 | cin>>n>>m; 48 | while(m--) 49 | { 50 | cin>>a>>b>>w; 51 | adj[a].push_back(make_pair(b,w)); 52 | adj[b].push_back(make_pair(a,w)); 53 | } 54 | for(int i=1;i<=n;i++) 55 | { 56 | dist1[i]=md; 57 | vis[i]=false; 58 | parent[i]=i; 59 | } 60 | djkstra(1,dist1); 61 | if(dist1[n]==md) 62 | { 63 | cout<<"-1"<v; 67 | int u=n; 68 | while(parent[u]!=u) 69 | { 70 | v.push_back(u); 71 | u =parent[u]; 72 | } 73 | v.push_back(1); 74 | vector::reverse_iterator it; 75 | for(it=v.rbegin();it!=v.rend();it++) 76 | { 77 | cout<<" "<<(*it); 78 | } 79 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | const int mx=1e5+1; 12 | 13 | map colors; 14 | map > card; 15 | vectoradj[mx]; 16 | 17 | int main() 18 | 19 | { 20 | int n,m,id,u,v; 21 | cin>>n>>m; 22 | for(int i=1;i<=n;i++) 23 | { 24 | cin>>id; 25 | colors[i]=id; 26 | } 27 | while(m--) 28 | { 29 | cin>>u>>v; 30 | adj[u].push_back(v); 31 | adj[v].push_back(u); 32 | } 33 | int mxsz=0; 34 | int mn_color=INT_MAX; 35 | for(int j=1;j<=n;j++) 36 | { 37 | int u=j; 38 | int v; 39 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | const int mx=101; 6 | int parent[mx]; 7 | int rnk[mx]={0}; 8 | int freq[mx]={0}; 9 | 10 | int Find(int x) 11 | { 12 | if(x!=parent[x]) 13 | parent[x]=Find(parent[x]); 14 | 15 | return parent[x]; 16 | } 17 | 18 | void Union(int x,int y) 19 | 20 | { 21 | int a=Find(x); 22 | int b=Find(y); 23 | 24 | if(a==b) return; 25 | 26 | if(rnk[a]>n>>m; 48 | int u,v,k; 49 | for(int i=0;i>k; 52 | if(k==0) 53 | { 54 | no_language++; 55 | continue; 56 | } 57 | cin>>u;// first elements of k elements 58 | freq[u]++; 59 | for(int j=1;j>v; 62 | freq[v]++; 63 | Union(u,v);// make set 64 | } 65 | } 66 | int ans=0; 67 | for(int i=1;i<=m;i++) 68 | { 69 | if(freq[i]==0) count++; 70 | else if(parent[i]==i) 71 | { 72 | ans++; 73 | } 74 | } 75 | ans=ans-1; 76 | if(count==m) cout< 2 | 3 | using namespace std; 4 | 5 | const int mx=1e5+1; 6 | bool vis[mx]; 7 | vectoradj[mx]; 8 | int n; 9 | 10 | bool star() 11 | { 12 | bool ans; 13 | int count1=0; 14 | int other=0; 15 | for(int i=1;i<=n;i++) 16 | { 17 | if(adj[i].size()==1) 18 | { 19 | count1++; 20 | } 21 | else if(adj[i].size()==n-1) 22 | { 23 | other++; 24 | } 25 | } 26 | if(other==1&&count1==n-1) 27 | { 28 | ans=true; 29 | } 30 | else{ 31 | ans=false; 32 | } 33 | return ans; 34 | } 35 | 36 | bool ring() 37 | { 38 | bool ans=true; 39 | for(int i=1;i<=n;i++) 40 | { 41 | if(adj[i].size()!=2) 42 | { 43 | ans=false; 44 | break; 45 | } 46 | } 47 | return ans; 48 | } 49 | 50 | bool bus() 51 | { 52 | bool ans; 53 | int one=0; 54 | int two=0; 55 | for(int i=1;i<=n;i++) 56 | { 57 | if(adj[i].size()==1) 58 | { 59 | one++; 60 | } 61 | else if(adj[i].size()==2) 62 | { 63 | two++; 64 | } 65 | } 66 | if(one==2 && two==n-2) 67 | { 68 | ans=true; 69 | } 70 | else{ 71 | ans=false; 72 | } 73 | return ans; 74 | } 75 | int main() 76 | 77 | { 78 | int m,k,u,v; 79 | cin>>n>>m; 80 | for(int i=0;i>u>>v; 83 | adj[u].push_back(v); 84 | adj[v].push_back(u); 85 | } 86 | if(star()) 87 | { 88 | cout<<"star topology"< 2 | 3 | using namespace std; 4 | 5 | 6 | int main() 7 | 8 | { 9 | int n,m,a,b; 10 | cin>>n>>m; 11 | int arr[n+1]={0}; 12 | while (m--) 13 | { 14 | cin>>a>>b; 15 | arr[a]++; 16 | arr[b]++; 17 | } 18 | int centre; 19 | for(int j=1;j<=n;j++) 20 | { 21 | 22 | if(arr[j]==0) 23 | { 24 | 25 | centre=j; 26 | } 27 | } 28 | cout<<(n-1)< 2 | 3 | using namespace std; 4 | 5 | const int mx=1e5+1; 6 | 7 | vector >adj[mx]; 8 | bool vis[mx]; 9 | vectorans; 10 | 11 | void dfs(int u,int prev) 12 | { 13 | for(int i=0;i>n; 40 | int m=n-1; 41 | while(m--) 42 | { 43 | cin>>u>>v>>w; 44 | adj[u].push_back(make_pair(v,w)); 45 | adj[v].push_back(make_pair(u,w)); 46 | } 47 | memset(vis,false,sizeof(vis)); 48 | dfs(1,-1);// for 1 there is no prev node 49 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | ios_base::sync_with_stdio(0); 9 | cin.tie(0); 10 | int t,n,p,tota; 11 | cin>>t; 12 | while(t--) 13 | { 14 | cin>>n>>p; 15 | int graph[n+1][n+1]; 16 | memset(graph,0,sizeof(graph)); 17 | tota=2*n+p; 18 | for(int i=1;i<=n&&tota>=0;i++) 19 | { 20 | for(int j=1;j<=n&&tota>=0;j++) 21 | { 22 | if(i!=j&&graph[i][j]==0){ 23 | cout< 2 | using namespace std; 3 | 4 | 5 | 6 | int main() 7 | 8 | { 9 | int n,k; 10 | scanf("%d%d",&n,&k); 11 | bool graph[n+1][n+1]; 12 | for(int i=1;i<=n;i++) 13 | { 14 | for(int j=1;j<=n;j++) 15 | { 16 | if(i!=j){ 17 | graph[i][j]=false; 18 | graph[j][i]=false; 19 | } 20 | } 21 | } 22 | int total_matches=(n*(n-1))>>1; 23 | int equal_win=total_matches/n; 24 | 25 | if(equal_win0;i++) 33 | { 34 | int count=k; 35 | for(int j=1;j<=n,total>0;j++) 36 | { 37 | if(i!=j) 38 | { 39 | 40 | if(graph[i][j]==false) 41 | { 42 | graph[i][j]=true; 43 | graph[j][i]=true; 44 | printf("%d %d\n",i,j); 45 | count--; 46 | total--; 47 | } 48 | } 49 | if(count==0) 50 | { 51 | break; 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Codeforces-427C - Checkposts.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int mx=1e5+1; 6 | const unsigned long long int mod=1e9+7; 7 | 8 | vectoradj[mx]; 9 | vectorrev[mx]; 10 | bool vis[mx]; 11 | bool nw_vis[mx]; 12 | vectorans; 13 | unsigned long long int cost[mx]; 14 | stacks; 15 | unsigned long long int value=0; 16 | mapm; 17 | 18 | void dfs(int u) 19 | { 20 | vis[u]=true; 21 | int v; 22 | for(int i=0;i>n; 57 | for(int i=1;i<=n;i++) 58 | { 59 | cin>>cost[i]; 60 | } 61 | cin>>e; 62 | while(e--) 63 | { 64 | cin>>u>>v; 65 | adj[u].push_back((v)); 66 | rev[v].push_back(u); 67 | } 68 | memset(vis,false,sizeof(vis)); 69 | for(int i=1;i<=n;i++){ 70 | if(!vis[i]){ 71 | dfs(i);} 72 | } 73 | memset(nw_vis,false,sizeof(nw_vis)); 74 | unsigned long long int ans=0; 75 | unsigned long long int count=1; 76 | while(!s.empty()) 77 | { 78 | int u=s.top(); 79 | value=INT_MAX; 80 | if(!nw_vis[u]) 81 | { 82 | dfs2(u); 83 | ans=(ans+value); 84 | if(m[value]>=0) 85 | count=((count%mod)*m[value])%mod; 86 | m.clear(); 87 | } 88 | s.pop(); 89 | } 90 | cout<<(ans)<<" "<<(count%mod)< 4 | 5 | using namespace std; 6 | 7 | const int mx=1e3+1; 8 | 9 | typedef pairpii; 10 | vectoradj[mx]; 11 | int cost[mx]; 12 | vectorpq; 13 | int ans=0; 14 | 15 | int main() 16 | 17 | { 18 | int n,m,a,b,c; 19 | cin>>n>>m; 20 | for(int i=1;i<=n;i++) 21 | { 22 | cin>>c; 23 | cost[i]=c; 24 | pq.push_back(make_pair(c,i)); 25 | } 26 | while(m--) 27 | { 28 | cin>>a>>b; 29 | adj[a].push_back(b); 30 | adj[b].push_back(a); 31 | } 32 | sort(pq.rbegin(),pq.rend()); 33 | int count=0; 34 | for(int i=0;i 3 | 4 | using namespace std; 5 | 6 | const int mx=1001; 7 | vectoradj[mx]; 8 | bool used[mx]; 9 | int ans=0; 10 | int pos[8][mx]; 11 | int level[mx]; 12 | 13 | void dfs(int v) 14 | { 15 | used[v]=true; 16 | for(int i=0;i>n>>k; 37 | for(int i=1;i<=k;i++) 38 | { 39 | for(int j=1;j<=n;j++) 40 | { 41 | cin>>a; 42 | pos[i][a]=j; 43 | } 44 | } 45 | 46 | for(int i=1;i<=n;i++) 47 | { 48 | for(int j=1;j<=n;j++) 49 | { 50 | if(i==j) continue; 51 | bool ok=true; 52 | for(int k1=1;k1<=k;k1++) 53 | { 54 | if(pos[k1][i]>=pos[k1][j]) ok=false; 55 | } 56 | if(ok) adj[i].push_back(j); 57 | } 58 | } 59 | for(int i=1;i<=n;i++) used[i]=false; 60 | for(int i=1;i<=n;i++) 61 | { 62 | if(!used[i]) 63 | dfs(i); 64 | } 65 | cout<<(ans)< 2 | 3 | using namespace std; 4 | 5 | int finally_visited[302]; 6 | int ans; 7 | int arr[302]; 8 | vectoradj[302]; 9 | bool vis[302]; 10 | void dfs(int index,bool*vis) 11 | { 12 | vis[index]=true; 13 | if(!finally_visited[index]) ans =min(arr[index],ans); // if that index has been replaced already by minimum number of that , so leave it 14 | 15 | for(int i=0;i>n; 30 | int position[n+1]; 31 | for(int i=0;i>arr[i]; 34 | position[arr[i]]=i; // taking position of a number !! 35 | } 36 | char c; 37 | for(int i=0;i>c; 42 | if(c=='1') 43 | { 44 | adj[i].push_back(j); // taking the indices which (i) may be replaced by j indices 45 | } 46 | } 47 | } 48 | memset(finally_visited,false,sizeof(finally_visited)); 49 | for(int i=0;i 3 | 4 | using namespace std; 5 | 6 | vector< pair >s; 7 | 8 | int main() 9 | 10 | { 11 | int n; 12 | cin>>n; 13 | int total_edges[n]; 14 | int xor_sum[n]; 15 | vectorleaf_node; 16 | for(int i=0;i>total_edges[i]>>xor_sum[i]; 19 | if(total_edges[i]==1) 20 | { 21 | leaf_node.push_back(i); 22 | } 23 | } 24 | int h=0; 25 | int u,v; 26 | while(h >::iterator it; 46 | for(it=s.begin();it!=s.end();it++) 47 | { 48 | cout<<(it->first)<<" "<<(it->second)< 2 | 3 | using namespace std; 4 | 5 | typedef pair pi; 6 | 7 | vectoradj[101]; 8 | bool vis[101]; 9 | 10 | bool dfs(int u,int dest,int clr) 11 | { 12 | vis[u]=true; 13 | if(u==dest) 14 | { 15 | return true; 16 | } 17 | for(int i=0;i>n; 41 | cin>>m; 42 | for(int i=1;i<=m;i++) 43 | { 44 | cin>>x>>y>>c; 45 | adj[x].push_back(make_pair(y,c)); 46 | adj[y].push_back(make_pair(x,c)); 47 | } 48 | int q; 49 | cin>>q; 50 | for(int i=1;i<=q;i++) 51 | { 52 | cin>>u>>v; 53 | int ans=0; 54 | for(int i=1;i<=100;i++) 55 | { 56 | memset(vis,false,sizeof(vis)); 57 | if(dfs(u,v,i)) 58 | { 59 | ans++; 60 | } 61 | } 62 | cout< 2 | 3 | using namespace std; 4 | 5 | typedef pair pi; 6 | 7 | bool vis[51][51]; 8 | char graph[51][51]; 9 | int dx[] = {1, -1, 0, 0}; // only left, right , up ,down are allowed here 10 | int dy[] = {0, 0, 1, -1}; 11 | int m,n; 12 | bool ok=false; 13 | 14 | void dfs(int i,int j,int frmi,int frmj,char co) 15 | { 16 | if(i<1||j<1||i>m||j>n) return; 17 | 18 | if(graph[i][j]!=co) return; 19 | 20 | if(vis[i][j]) {ok=true; return ;} 21 | 22 | vis[i][j]=true; 23 | 24 | for(int y=0;y<4;y++) 25 | { 26 | int nxti=i+dx[y]; 27 | int nxtj=j+dy[y]; 28 | 29 | if(nxti==frmi && nxtj==frmj) 30 | continue;// it doesn't go back from where it comes from 31 | 32 | dfs(nxti,nxtj,i,j,co); 33 | } 34 | } 35 | 36 | 37 | int main() 38 | 39 | { 40 | ios::sync_with_stdio(false); 41 | cin.tie(NULL); 42 | cout.tie(NULL); 43 | memset(vis,false,sizeof(vis)); 44 | int x,y,u,v; 45 | char c; 46 | cin>>m>>n; 47 | for(int i=1;i<=m;i++) 48 | 49 | { 50 | for(int j=1;j<=n;j++) 51 | 52 | { 53 | cin>>graph[i][j]; 54 | } 55 | } 56 | for(int i=1;i<=m;i++) 57 | { 58 | for(int j=1;j<=n;j++) 59 | { 60 | char z=graph[i][j]; 61 | //cout< 2 | 3 | using namespace std; 4 | 5 | const int mx=1e5+1; 6 | typedef pair pi; 7 | 8 | 9 | int main() 10 | 11 | { 12 | ios::sync_with_stdio(false); 13 | cin.tie(NULL); 14 | cout.tie(NULL); 15 | int n,d,k1,k2; 16 | cin>>n; 17 | queuea; 18 | queueb; 19 | cin>>k1; 20 | while(k1--) 21 | { 22 | cin>>d; 23 | a.push(d); 24 | } 25 | cin>>k2; 26 | while(k2--) 27 | { 28 | cin>>d; 29 | b.push(d); 30 | } 31 | int win; 32 | int count=0; 33 | while(true) 34 | { 35 | int frst=a.front(); 36 | a.pop(); 37 | int scnd=b.front(); 38 | b.pop(); 39 | count++; 40 | if(frst>scnd) 41 | { 42 | a.push(scnd); 43 | a.push(frst); 44 | } 45 | else 46 | { 47 | b.push(frst); 48 | b.push(scnd); 49 | } 50 | if(a.empty()) 51 | { 52 | win=2; 53 | break; 54 | } 55 | if(b.empty()) 56 | { 57 | win=1; 58 | break; 59 | } 60 | if(count>100000) 61 | { 62 | cout<<"-1"< 2 | 3 | using namespace std; 4 | 5 | typedef unsigned long long ll; 6 | 7 | const int mx =4001; 8 | 9 | vectorgraph[mx]; 10 | bool mat[mx][mx]; 11 | int recog[mx]; 12 | 13 | int main() 14 | 15 | { 16 | ios::sync_with_stdio(false); 17 | cin.tie(NULL); 18 | cout.tie(NULL); 19 | int n,m; 20 | cin>>n>>m; 21 | int u,v; 22 | memset(mat,false,sizeof(mat)); 23 | memset(recog,0,sizeof(recog)); 24 | while(m--) 25 | { 26 | cin>>u>>v; 27 | graph[u].push_back(v); 28 | graph[v].push_back(u); 29 | mat[u][v]=true; 30 | mat[v][u]=true; 31 | recog[u]++; 32 | recog[v]++; 33 | } 34 | int ans=INT_MAX; 35 | int res=0; 36 | for(int i=1;i<=n;i++) 37 | { 38 | if(graph[i].size()>1){ 39 | for(int j=1;j<=n;j++) 40 | { 41 | if(mat[i][j]&&graph[j].size()>1&&j!=i){ 42 | for(int k=1;k<=n;k++) 43 | { 44 | if(k!=i&&k!=j&&graph[k].size()>1&&mat[k][j]&&mat[k][i]) 45 | { 46 | res=(recog[i]-2+recog[j]-2+recog[k]-2); 47 | ans=min(ans,res); 48 | } 49 | } 50 | } 51 | } 52 | } 53 | } 54 | if(ans==INT_MAX) cout<<"-1"< 2 | 3 | using namespace std; 4 | 5 | const int mx=1e5+1; 6 | typedef pair pi; 7 | vectoradj[mx]; 8 | bool vis[mx]; 9 | int cat[mx]; 10 | int n,m; 11 | int ans=0; 12 | int parent[mx]={-1}; 13 | int dp[mx]={0}; 14 | int mx_consecutive[mx]={0}; 15 | 16 | void dfs(int u,int prev) 17 | { 18 | vis[u]=true; 19 | if(cat[u]) 20 | { 21 | dp[u]=1+dp[prev]; 22 | } 23 | else{ 24 | dp[u]=0; 25 | } 26 | mx_consecutive[u]=max(mx_consecutive[prev],dp[u]); 27 | bool is_leaf=true; 28 | for(int i=0;i>n>>m; 50 | for(int i=1;i<=n;i++) 51 | { 52 | cin>>cat[i]; 53 | } 54 | for(int j=1;j<=n-1;j++) 55 | { 56 | cin>>u>>v; 57 | adj[u].push_back((v)); 58 | adj[v].push_back((u)); 59 | } 60 | memset(vis,false,sizeof(vis)); 61 | dfs(1,-1); 62 | cout< 4 | 5 | using namespace std; 6 | 7 | const int mx=401; 8 | 9 | typedef pairpii; 10 | vectorrail[mx]; 11 | vectorbus[mx]; 12 | sets; 13 | int dist_rail[mx]; 14 | int dist_bus[mx]; 15 | bool vis[mx]; 16 | 17 | void djkstra(vectoradj[],int *dist) 18 | 19 | { 20 | priority_queue,greater >pq; 21 | pq.push(make_pair(0,1)); 22 | dist[1]=0; 23 | while(!pq.empty()) 24 | { 25 | int u=pq.top().second; 26 | pq.pop(); 27 | if(vis[u]) continue; 28 | 29 | vis[u]=true; 30 | for(int i=0;idist[u]+weight) 35 | { 36 | dist[v]=dist[u]+weight; 37 | pq.push(make_pair(dist[v],v)); 38 | } 39 | } 40 | } 41 | } 42 | 43 | int main() 44 | 45 | { 46 | int n,m,a,b; 47 | cin>>n>>m; 48 | while(m--) 49 | { 50 | cin>>a>>b; 51 | rail[a].push_back(make_pair(b,1)); 52 | rail[b].push_back(make_pair(a,1)); 53 | s.insert(make_pair(min(a,b),max(a,b))); 54 | } 55 | for(int i=1;i<=n;i++) 56 | { 57 | dist_rail[i]=INT_MAX; 58 | dist_bus[i]=INT_MAX; 59 | vis[i]=false; 60 | } 61 | djkstra(rail,dist_rail); 62 | for(int i=1;i<=n;i++) 63 | { 64 | vis[i]=false; 65 | } 66 | for(int i=1;i<=n;i++) 67 | { 68 | for(int j=i+1;j<=n;j++) 69 | { 70 | pii p; 71 | p.first=i; 72 | p.second=j; 73 | if(!s.count(p)) 74 | { 75 | bus[i].push_back(make_pair(j,1)); 76 | bus[j].push_back(make_pair(i,1)); 77 | } 78 | } 79 | } 80 | djkstra(bus,dist_bus); 81 | if(dist_bus[n]==INT_MAX||dist_rail[n]==INT_MAX) 82 | { 83 | cout<<"-1"< 2 | 3 | using namespace std; 4 | 5 | typedef pair pi; 6 | 7 | const int mx=1e5; 8 | 9 | int main() 10 | 11 | { 12 | ios::sync_with_stdio(false); 13 | cin.tie(NULL); 14 | cout.tie(NULL); 15 | int n,u,v; 16 | mapedge; 17 | cin>>n; 18 | int cost[n+1]; 19 | for(int i=0;i>u>>v; 22 | edge[i]=(make_pair(u,v)); 23 | } 24 | for(int j=1;j<=n;j++) 25 | { 26 | cin>>cost[j]; 27 | } 28 | mapnotequal; 29 | int y=0; 30 | bool onetime=true; 31 | for(int k=0;k 2 | 3 | using namespace std; 4 | 5 | const int mx=1e3+1; 6 | bool vis[mx]; 7 | vectoradj[mx]; 8 | int count_connected=0; 9 | int total_connected=0; 10 | int max_connected=0; 11 | int total_pair_edges=0; 12 | 13 | void dfs(int u) 14 | { 15 | count_connected++; 16 | vis[u]=true; 17 | for(int i=0;i>n>>m>>k; 31 | int special[k]; 32 | for(int i=0;i>special[i]; 35 | } 36 | for(int i=0;i>u>>v; 39 | adj[u].push_back(v); 40 | adj[v].push_back(u); 41 | } 42 | for(int j=0;j<=n;j++) 43 | { 44 | vis[j]=false; 45 | } 46 | for(int i=0;i 4 | 5 | using namespace std; 6 | 7 | const int mx=1e5+1; 8 | 9 | vectorzero; 10 | vectorone; 11 | int color[mx]; 12 | vectornodes[mx]; 13 | 14 | bool isPosiible(int vertex) 15 | { 16 | queueQ; 17 | Q.push(vertex); 18 | color[vertex]=0; 19 | zero.push_back(vertex); 20 | while(!Q.empty()) 21 | { 22 | int u=Q.front(); 23 | Q.pop(); 24 | for(int i=0;i>n>>m; 58 | int ver=INT_MAX; 59 | while(m--) 60 | { 61 | cin>>a>>b; 62 | ver=min(a,min(b,ver)); 63 | nodes[a].push_back(b); 64 | nodes[b].push_back(a); 65 | } 66 | for(int i=1;i<=n;i++) 67 | { 68 | color[i]=-1; 69 | } 70 | for(int i=1;i<=n;i++){ 71 | if(color[i]==-1){ 72 | if(nodes[i].size()==0){ // This node has no connection with other nodes 73 | continue;} 74 | 75 | if(!isPosiible(i)) 76 | { 77 | cout<<"-1"< 2 | 3 | using namespace std; 4 | 5 | typedef unsigned long long ll; 6 | 7 | vectoradj[51]; 8 | bool vis[51]; 9 | int depth=1; 10 | 11 | void dfs(int u) 12 | { 13 | vis[u]=true; 14 | for(int i=0;i>n; 32 | cin>>m; 33 | for(int i=1;i<=m;i++) 34 | { 35 | cin>>x>>y; 36 | adj[x].push_back(y); 37 | adj[y].push_back(x); 38 | } 39 | memset(vis,false,sizeof(vis)); 40 | long long sum=0; 41 | for(int i=1;i<=n;i++) 42 | { 43 | if(!vis[i]) 44 | { 45 | dfs(i); 46 | sum++; 47 | } 48 | } 49 | long long ans= pow(2,n-sum); 50 | cout< 3 | 4 | using namespace std; 5 | 6 | const int mx=501; 7 | 8 | typedef unsigned long long int ll; 9 | 10 | ll adj[mx][mx]; 11 | ll dist[mx][mx]; 12 | ll v; 13 | int node=INT_MAX; 14 | ll sum=0; 15 | ll md=1e14; 16 | 17 | vectorans; 18 | bool is_delt[mx]; 19 | 20 | int main() 21 | 22 | { 23 | ios_base::sync_with_stdio(false); 24 | cin.tie(NULL); 25 | int r; 26 | cin>>v; 27 | for(int i=1;i<=v;i++) 28 | { 29 | for(int j=1;j<=v;j++) 30 | { 31 | cin>>adj[i][j]; 32 | } 33 | } 34 | int delt_vertex[v+1]; 35 | for(int i=v;i>=1;i--) 36 | { 37 | cin>>r; 38 | delt_vertex[i]=r; 39 | } 40 | memset(is_delt,false,sizeof(is_delt)); 41 | ll sum[v+1]={0}; 42 | for(int k=1;k<=v;k++) 43 | { 44 | int vertex=delt_vertex[k]; 45 | is_delt[vertex]=true; 46 | for(int i=1;i<=v;i++) 47 | { 48 | for(int j=1;j<=v;j++) 49 | { 50 | adj[i][j]=min(adj[i][j],adj[i][vertex]+adj[vertex][j]); 51 | if(is_delt[i]&&is_delt[j]) 52 | { 53 | sum[k]+=adj[i][j]; 54 | } 55 | } 56 | } 57 | } 58 | for(int i=v;i>=1;i--) 59 | { 60 | cout< 7 | 8 | using namespace std; 9 | 10 | struct str 11 | { 12 | int x,y; 13 | int sz; 14 | }; 15 | 16 | const int mx=52; 17 | 18 | bool vis[mx][mx]; 19 | char mat[mx][mx]; 20 | bool border_area; 21 | int conts=0; 22 | 23 | void dfs(int x,int y,int n,int m) 24 | { 25 | if(x<1 || x>n || y<1 || y>m) return; 26 | if(vis[x][y]) return; 27 | if(y==1||y==m||x==n||x==1) {border_area=true;return;} 28 | /// adjacent 29 | conts++; 30 | vis[x][y]=true; 31 | if(mat[x][y+1]=='.') dfs(x,y+1,n,m); 32 | if(mat[x][y-1]=='.') dfs(x,y-1,n,m); 33 | if(mat[x+1][y]=='.') dfs(x+1,y,n,m); 34 | if(mat[x-1][y]=='.') dfs(x-1,y,n,m); 35 | } 36 | 37 | void fill_up(int x,int y,int n,int m) 38 | { 39 | if(x<1 || x>n || y<1 || y>m) return; 40 | if(vis[x][y]) return; 41 | /// adjacent 42 | vis[x][y]=true; 43 | mat[x][y]='*'; 44 | if(mat[x][y+1]=='.') fill_up(x,y+1,n,m); 45 | if(mat[x][y-1]=='.') fill_up(x,y-1,n,m); 46 | if(mat[x+1][y]=='.') fill_up(x+1,y,n,m); 47 | if(mat[x-1][y]=='.') fill_up(x-1,y,n,m); 48 | } 49 | 50 | bool comp(str a,str b) 51 | { 52 | return (a.sz>n>>m>>k; 60 | 61 | for(int i=1;i<=n;i++) 62 | { 63 | for(int j=1;j<=m;j++) 64 | { 65 | cin>>mat[i][j]; 66 | } 67 | } 68 | 69 | vector< str >v; 70 | memset(vis,false,sizeof(vis)); 71 | 72 | for(int i=1;i<=n;i++) 73 | { 74 | for(int j=1;j<=m;j++) 75 | { 76 | if(!vis[i][j] && mat[i][j]=='.') 77 | { 78 | border_area=false; 79 | conts=0; 80 | dfs(i,j,n,m); 81 | if(!border_area) 82 | { 83 | str st; 84 | st.x=i; 85 | st.y=j; 86 | st.sz=conts; 87 | v.push_back(st); 88 | } 89 | } 90 | } 91 | 92 | } 93 | cout< 10 | 11 | using namespace std; 12 | 13 | const int mx=1e5+1; 14 | 15 | bool visited[mx]; 16 | int firstarr[mx]; 17 | int secondarr[mx]; 18 | vector swappable_elemnts_of_first; 19 | vector swappable_elemnts_of_second; 20 | vectorv[mx]; 21 | 22 | void dfs(int i) 23 | { 24 | visited[i]=true; 25 | swappable_elemnts_of_first.push_back(firstarr[i]); 26 | swappable_elemnts_of_second.push_back(secondarr[i]); 27 | vector::iterator it; 28 | for(it=v[i].begin();it!=v[i].end();it++) 29 | { 30 | if(!visited[(*it)]) 31 | { 32 | dfs((*it)); 33 | } 34 | } 35 | } 36 | 37 | int main() 38 | 39 | { 40 | int t,n,m,a,b; 41 | cin>>t; 42 | while(t--) 43 | { 44 | cin>>n>>m; 45 | for(int i=1;i<=n;i++) 46 | { 47 | cin>>firstarr[i]; 48 | } 49 | for(int j=1;j<=n;j++) 50 | { 51 | cin>>secondarr[j]; 52 | } 53 | while(m--) 54 | { 55 | cin>>a>>b; 56 | v[a].push_back(b); 57 | v[b].push_back(a); 58 | } 59 | memset(visited,false,sizeof(visited)); 60 | bool ok=true; 61 | for(int k=1;k<=n;k++) 62 | { 63 | if(!visited[k]) 64 | { 65 | swappable_elemnts_of_first.clear(); 66 | swappable_elemnts_of_second.clear(); 67 | dfs(k); 68 | sort(swappable_elemnts_of_first.begin(),swappable_elemnts_of_first.end()); 69 | sort(swappable_elemnts_of_second.begin(),swappable_elemnts_of_second.end()); 70 | if(swappable_elemnts_of_first!=swappable_elemnts_of_second) 71 | { 72 | ok=false; 73 | //cout<<"NO"< 2 | 3 | using namespace std; 4 | 5 | bool visited[1001]; 6 | int dist[1001]; 7 | 8 | void bfs(vectorv[]) 9 | { 10 | visited[1]=true; 11 | queueq; 12 | dist[1]=0; 13 | q.push(1); 14 | while(!q.empty()) 15 | { 16 | int temp=q.front(); 17 | q.pop(); 18 | vector::iterator it; 19 | for(it=v[temp].begin();it!=v[temp].end();it++) 20 | { 21 | if(!visited[(*it)]) 22 | { 23 | dist[(*it)]=dist[temp]+1; 24 | q.push((*it)); 25 | visited[(*it)]=true; 26 | } 27 | } 28 | } 29 | } 30 | 31 | int main() 32 | 33 | { 34 | int n,m,u,t,q,gf; 35 | cin>>n; 36 | m=n-1; 37 | vectorv[n+1]; 38 | while(m--) 39 | { 40 | cin>>u>>t; 41 | v[u].push_back(t); 42 | v[t].push_back(u); 43 | } 44 | cin>>q; 45 | vectorgirls; 46 | while(q--) 47 | { 48 | cin>>gf; 49 | girls.push_back(gf); 50 | } 51 | 52 | for(int i=0;i<=1000;i++) 53 | { 54 | dist[i]=INT_MAX; 55 | visited[i]=false; 56 | } 57 | bfs(v); 58 | int girldis=dist[girls[0]]; 59 | int girlid=girls[0]; 60 | for(int i=1;idist[girls[i]]) 64 | { 65 | girldis=dist[girls[i]]; 66 | girlid=girls[i]; 67 | } 68 | } 69 | cout< 2 | 3 | using namespace std; 4 | 5 | const int mx=1e6+1; 6 | 7 | typedef pairpii; 8 | int n,m; 9 | vectoradj[mx]; 10 | 11 | void dijkstra(int source,int dist[]) 12 | { 13 | priority_queue,greater > pq; 14 | bool vis[n+1]; 15 | for(int i=0;i<=n;i++) 16 | { 17 | vis[i]=false; 18 | } 19 | pq.push(make_pair(0,source)); 20 | dist[source]=0; 21 | while(!pq.empty()) 22 | { 23 | int u=pq.top().second; 24 | pq.pop(); 25 | if(vis[u]) 26 | continue; 27 | 28 | vis[u]=true; 29 | vector::iterator it; 30 | for(it=adj[u].begin();it!=adj[u].end();it++) 31 | { 32 | int v=(*it).first; 33 | int weight =(*it).second; 34 | //cout<dist[u]+weight)) 36 | { 37 | dist[v]=dist[u]+weight; 38 | pq.push(make_pair(dist[v],v)); 39 | } 40 | } 41 | } 42 | } 43 | 44 | int main() 45 | 46 | { 47 | int a,u,v,l,s,d,k,x; 48 | cin>>n>>m>>k>>x; 49 | vectorcity; 50 | for(int i=0;i>a; 53 | city.push_back(a); 54 | } 55 | while(m--) 56 | { 57 | cin>>u>>v>>l; 58 | adj[u].push_back(make_pair(v,l)); 59 | adj[v].push_back(make_pair(u,l)); 60 | } 61 | cin>>s>>d; 62 | int dist1[n+1]; 63 | int dist2[n+1]; 64 | for(int i=0;i<=n;i++) 65 | { 66 | dist1[i]=INT_MAX; 67 | dist2[i]=INT_MAX; 68 | } 69 | dijkstra(s,dist1); 70 | dijkstra(d,dist2); 71 | 72 | int ans=INT_MAX; 73 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | const int mx=1e5+1; 6 | 7 | typedef pairpii; 8 | 9 | bool vis[mx]={false}; 10 | long dist[mx]; 11 | vectoradj[mx]; 12 | priority_queue,greater > pq; 13 | 14 | void dijkstra() 15 | { 16 | 17 | while(!pq.empty()) 18 | { 19 | int u=pq.top().second; 20 | pq.pop(); 21 | if(vis[u]) 22 | continue; 23 | 24 | vis[u]=true; 25 | vector::iterator it; 26 | for(it=adj[u].begin();it!=adj[u].end();it++) 27 | { 28 | int v=(*it).first; 29 | int weight =(*it).second; 30 | //cout<dist[u]+weight)) 32 | { 33 | dist[v]=dist[u]+weight; 34 | pq.push(make_pair(dist[v],v)); 35 | //cout<>n>>m>>k>>q; 46 | int u,v,w; 47 | while(m--) 48 | { 49 | cin>>u>>v>>w; 50 | adj[u].push_back(make_pair(v,w)); 51 | adj[v].push_back(make_pair(u,w)); 52 | } 53 | int x; 54 | for(int i=1;i<=n;i++) 55 | { 56 | 57 | dist[i]=1000000007; 58 | } 59 | while(k--) 60 | { 61 | cin>>x; 62 | pq.push(make_pair(0,x)); // exma centres 63 | dist[x]=0; 64 | } 65 | dijkstra(); 66 | while(q--) 67 | { 68 | cin>>x; 69 | if(dist[x]==1000000007) 70 | { 71 | cout<<"-1"< 2 | 3 | using namespace std; 4 | 5 | int graph[91][91]={0}; 6 | int resgraph[91][91]={0}; 7 | int parent[91]; 8 | 9 | int mx_flow=0; 10 | 11 | bool bfs(int s,int t) 12 | { 13 | bool visited[91]={false}; 14 | queueq; 15 | q.push(s); 16 | visited[s]=true; 17 | parent[s]=-1; 18 | while(!q.empty()) 19 | { 20 | int u=q.front(); 21 | q.pop(); 22 | for(int v=0;v<91;v++) 23 | { 24 | if(visited[v]==false && resgraph[u][v]>0) 25 | { 26 | q.push(v); 27 | parent[v]=u; 28 | visited[v]=true; 29 | } 30 | } 31 | } 32 | return (visited[t]==true); 33 | } 34 | 35 | void fordFulker(int s,int t) 36 | { 37 | int v; 38 | for(int u=0;u<91;u++) 39 | { 40 | for(int v=0;v<91;v++) 41 | { 42 | resgraph[u][v]=graph[u][v]; 43 | } 44 | } 45 | while(bfs(s,t)==true) 46 | { 47 | int path_flow=INT_MAX; 48 | for(v=t;v!=s;v=parent[v]) 49 | { 50 | int u=parent[v]; 51 | path_flow=min(path_flow,resgraph[u][v]); 52 | } 53 | for(v=t;v!=s;v=parent[v]) 54 | { 55 | int u=parent[v]; 56 | resgraph[u][v]-=path_flow; 57 | resgraph[v][u]+=path_flow; 58 | } 59 | //cout<>total; 69 | char a,b; 70 | int cst; 71 | int source,sink; 72 | char c1,c2; 73 | c1='S'; 74 | c2='T'; 75 | source=(int)c1; 76 | sink=(int)c2; 77 | while(total--) 78 | { 79 | cin>>a>>b>>cst; 80 | int u=(int)a; 81 | int v=(int)b; 82 | graph[u][v]=cst; 83 | } 84 | fordFulker(source,sink); 85 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | 7 | { 8 | int tc,n,m,a,b; 9 | cin>>tc; 10 | while(tc--) 11 | { 12 | cin>>n>>m; 13 | while(m--) 14 | { 15 | cin>>a>>b; 16 | } 17 | cout<<(n-1)< 2 | 3 | using namespace std; 4 | 5 | const int mx=1e5+1; 6 | 7 | vector>adj[mx]; 8 | typedef pair pii; 9 | bool vis[mx]; 10 | int dist[mx]; 11 | 12 | void djkstra(int source) 13 | { 14 | priority_queue,greater >pq; 15 | dist[source]=0; 16 | pii p; 17 | p.first=0; 18 | p.second =1; 19 | pq.push(p); 20 | while(!pq.empty()) 21 | { 22 | p= pq.top(); 23 | int t =p.first; 24 | pq.pop(); 25 | int u= p.second; 26 | if(vis[u]) continue; 27 | 28 | vis[u]=true; 29 | 30 | for(int i=0;itt) 43 | { 44 | dist[v]=tt; 45 | pq.push(make_pair(tt,v)); // tt holds for minimum time to reach node v 46 | } 47 | } 48 | } 49 | 50 | } 51 | 52 | int main() 53 | 54 | { 55 | int n,e,q,u,v,tm; 56 | cin>>n>>e>>q; 57 | while(e--) 58 | { 59 | cin>>u>>v; 60 | adj[u].push_back(make_pair(v,0)); // u-> v edge cost holds for even seconds which represents 0 61 | adj[v].push_back(make_pair(u,1)); // v-> u edge cost holds for odd seconds which represents 1 62 | } 63 | memset(vis,false,sizeof(vis)); 64 | for(int i=0;i<=n;i++) 65 | { 66 | dist[i]=INT_MAX; 67 | } 68 | djkstra(1); 69 | while(q--) 70 | { 71 | cin>>tm; 72 | if(tm 2 | 3 | using namespace std; 4 | const int mn=1e5+1; 5 | long int dist[mn]; 6 | bool visited[mn]; 7 | void bfs(int s,int d,vectorv[]) 8 | { 9 | visited[1]=true; 10 | queueq; 11 | dist[1]=0; 12 | q.push(1); 13 | while(!q.empty()) 14 | { 15 | int u=q.front(); 16 | q.pop(); 17 | for(int it=0;it>t; 35 | while(t--) 36 | { 37 | cin>>n>>m; 38 | vectorv[n+1]; 39 | for(int i=0;i>a>>b; 42 | v[a].push_back(b); 43 | v[b].push_back(a); 44 | } 45 | for(int i=0;i<=n;i++) 46 | { 47 | dist[i]=0; 48 | visited[i]=false; 49 | } 50 | bfs(1,n,v); 51 | cout< 2 | 3 | using namespace std; 4 | 5 | typedef pair< int ,int>pii; 6 | const int mx = 1e6+5; 7 | vectoradj[mx]; 8 | bool marked[mx]={false}; 9 | unsigned long long mncst; 10 | priority_queuevec; 11 | 12 | void prim_algorithm(int x) 13 | { 14 | mncst=0; 15 | priority_queue,greater >q; // increasing order 16 | pii p; 17 | q.push(make_pair(0,x)); 18 | while(!q.empty()) 19 | { 20 | p=q.top(); 21 | q.pop(); 22 | int x=p.second; 23 | if(marked[x]==true){ 24 | continue;} 25 | 26 | mncst+=(unsigned long long)(p.first); 27 | vec.push((p.first)); 28 | marked[x]=true; 29 | 30 | for(int i=0;i>N>>M>>K; 48 | while(M--) 49 | { 50 | cin>>U>>V>>W; 51 | adj[U].push_back(make_pair(W,V)); 52 | adj[V].push_back(make_pair(W,U)); 53 | } 54 | prim_algorithm(1); 55 | bool disconnected=false; 56 | for(int l=1;l<=N;l++) 57 | { 58 | if(marked[l]==false) 59 | { 60 | disconnected=true; 61 | } 62 | } 63 | if(disconnected) 64 | { 65 | cout<<"-1"<K) 77 | { 78 | unsigned long long tp=(unsigned long long) (vec.top()); 79 | vec.pop(); 80 | cost=cost-tp; 81 | cost++;// for super roads 1 82 | count++; 83 | } 84 | if(cost<=K) 85 | { 86 | cout< 2 | 3 | using namespace std; 4 | 5 | int graph[153][153]={0}; 6 | int resgraph[153][153]={0}; 7 | int parent[153]; 8 | 9 | int mx_flow=0; 10 | 11 | bool bfs(int s,int t) 12 | { 13 | bool visited[153]={false}; 14 | queueq; 15 | q.push(s); 16 | visited[s]=true; 17 | parent[s]=-1; 18 | while(!q.empty()) 19 | { 20 | int u=q.front(); 21 | q.pop(); 22 | for(int v=0;v<153;v++) 23 | { 24 | if(visited[v]==false && resgraph[u][v]>0) 25 | { 26 | q.push(v); 27 | parent[v]=u; 28 | visited[v]=true; 29 | } 30 | } 31 | } 32 | return (visited[t]==true); 33 | } 34 | 35 | void fordFulker(int s,int t) 36 | { 37 | int v; 38 | for(int u=0;u<153;u++) 39 | { 40 | for(int v=0;v<153;v++) 41 | { 42 | resgraph[u][v]=graph[u][v]; 43 | } 44 | } 45 | while(bfs(s,t)==true) 46 | { 47 | int path_flow=INT_MAX; 48 | for(v=t;v!=s;v=parent[v]) 49 | { 50 | int u=parent[v]; 51 | path_flow=min(path_flow,resgraph[u][v]); 52 | } 53 | for(v=t;v!=s;v=parent[v]) 54 | { 55 | int u=parent[v]; 56 | resgraph[u][v]-=path_flow; 57 | resgraph[v][u]+=path_flow; 58 | } 59 | mx_flow=mx_flow+path_flow; 60 | } 61 | } 62 | 63 | int main() 64 | 65 | { 66 | int test,V,total; 67 | cin>>test; 68 | while(test--){ 69 | cin>>V>>total; 70 | int u,v,k; 71 | int cst; 72 | int source,sink; 73 | while(total--) 74 | { 75 | cin>>u>>v; 76 | graph[u][v]=1; 77 | } 78 | cin>>source>>sink>>k; 79 | fordFulker(source,sink); 80 | if(mx_flow>=k+1)// with k friends , they are totally k+1 friends 81 | { 82 | cout<<"YES"< 2 | 3 | using namespace std; 4 | 5 | int total=0; 6 | const int mx=1e3+1; 7 | int arr[mx][mx]; 8 | 9 | void dfs(int i,int j,int n,int m) 10 | { 11 | if(i<0 || i>=n || j<0 || j>=m || arr[i][j]==0) {return ;} 12 | 13 | arr[i][j]=0; 14 | total++; 15 | //cout<>t; 30 | while(t--) 31 | { 32 | cin>>n>>m; 33 | for(int i=0;i>arr[i][j]; 38 | } 39 | } 40 | int count=0; 41 | int ans=0; 42 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int finally_visited[102]; 6 | int ans; 7 | int arr[102]; 8 | vectoradj[102]; 9 | bool vis[102]; 10 | void dfs(int index,bool*vis) 11 | { 12 | vis[index]=true; 13 | if(!finally_visited[index]) ans =min(arr[index],ans); // if that index has been replaced already by minimum number of that , so leave it 14 | 15 | for(int i=0;i>n; 30 | int position[n+1]; 31 | for(int i=0;i>arr[i]; 34 | position[arr[i]]=i; // taking position of a number !! 35 | } 36 | char c; 37 | for(int i=0;i>c; 42 | if(c=='Y') 43 | { 44 | adj[i].push_back(j); // taking the indices which (i) may be replaced by j indices 45 | } 46 | } 47 | } 48 | memset(finally_visited,false,sizeof(finally_visited)); 49 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | typedef pair< int ,int>pii; 6 | const int mx = 1501; 7 | const unsigned long long mod= 1e9+7; 8 | bool marked[mx]={false}; 9 | unsigned long long mncst=1; 10 | 11 | void prim_algorithm(int x,vectoradj[]) 12 | { 13 | mncst=1; 14 | priority_queue,greater >q; // increasing order 15 | pii p; 16 | q.push(make_pair(1,x)); 17 | while(!q.empty()) 18 | { 19 | p=q.top(); 20 | q.pop(); 21 | int x=p.second; 22 | if(marked[x]==true){ 23 | continue;} 24 | 25 | mncst= (mncst*((unsigned long long)(p.first)))%mod; 26 | marked[x]=true; 27 | 28 | for(int i=0;i>t; 44 | while(t--){ 45 | int N,M,U,V,W; 46 | unsigned long long K; 47 | cin>>N>>M; 48 | vectoradj[N+1]; 49 | while(M--) 50 | { 51 | cin>>U>>V>>W; 52 | adj[U].push_back(make_pair(W,V)); 53 | adj[V].push_back(make_pair(W,U)); 54 | } 55 | prim_algorithm(1,adj); 56 | unsigned long long cost=mncst; 57 | mncst=1; 58 | cout< 2 | 3 | using namespace std; 4 | 5 | const int mx=1e2+1; 6 | 7 | bool vis[mx]; 8 | int disc[mx]; 9 | int low[mx]; 10 | int parent[mx]; 11 | bool ap[mx]; 12 | vectoradj[mx]; 13 | 14 | void apufind(int u) 15 | { 16 | static int time=0; 17 | disc[u]=low[u]=time++; 18 | int child=0; 19 | vis[u]=true; 20 | int i; 21 | for(i=0;i1) 31 | { 32 | ap[u]=true; // root 33 | } 34 | if( parent[u]!=-1 && low[v]>=disc[u]) 35 | { 36 | ap[u]=true; // there is a back edge if it is disconnected , so connected component will be increased 37 | } 38 | } 39 | else if(parent[u]!=v) 40 | { 41 | low[u]=min(low[u],disc[v]); 42 | } 43 | 44 | } 45 | } 46 | 47 | int main() 48 | 49 | { 50 | int n,m,q,a,b,index; 51 | cin>>n>>m; 52 | while(m--) 53 | { 54 | cin>>a>>b; 55 | adj[a].push_back(b); 56 | adj[b].push_back(a); 57 | } 58 | for(int i=0;i<=n;i++) 59 | { 60 | parent[i]=-1; 61 | vis[i]=false; 62 | ap[i]=false; 63 | } 64 | for(int i=1;i<=n;i++) 65 | { 66 | if(vis[i]==false) 67 | { 68 | apufind(i); 69 | } 70 | } 71 | cin>>q; 72 | while(q--) 73 | { 74 | cin>>index; 75 | int ans= (int) (adj[index].size());// when one point is articulation point , if it removes - connected to it will be disconnected 76 | if(ap[index]) 77 | { 78 | cout<<"Satisfied "< 2 | 3 | using namespace std; 4 | 5 | const int mx= 1e5+1; 6 | 7 | int parent[mx]; 8 | bool vis[mx]; 9 | int disc[mx]; 10 | int low[mx]; 11 | bool apu[mx]; 12 | map < int, pair >edges; 13 | map < pair , int> bridge; 14 | vectoradj[mx]; 15 | set>s; 16 | 17 | void apufind(int u) 18 | { 19 | static int time =0; 20 | disc[u]=low[u]=time++; 21 | vis[u]=true; 22 | for(int i=0;i<(int)adj[u].size();i++) 23 | { 24 | int v=adj[u][i]; 25 | if(!vis[v]) 26 | { 27 | parent[v]=u; 28 | apufind(v); 29 | low[u]=min(low[u],low[v]); 30 | 31 | if(low[v]>disc[u]) 32 | { 33 | pairp = make_pair(min(u,v),max(v,u)); 34 | bridge[p]=1; 35 | } 36 | } 37 | else if( v!=parent[u]) 38 | { 39 | low[u]=min(disc[v],low[u]); 40 | } 41 | } 42 | } 43 | 44 | int main() 45 | 46 | { 47 | int n,m; 48 | cin>>n>>m; 49 | int u,v; 50 | int i=1; 51 | while(m--) 52 | { 53 | cin>>u>>v; 54 | adj[u].push_back(v); 55 | adj[v].push_back(u); 56 | edges[i++]=make_pair(min(u,v),max(v,u)); 57 | } 58 | for(int i=1;i<=n;i++) 59 | { 60 | parent[i]=-1; 61 | vis[i]=false; 62 | } 63 | for(int i=1;i<=n;i++) 64 | { 65 | if(!vis[i]){ 66 | apufind(i);} 67 | } 68 | int p,g; 69 | cin>>p; 70 | while(p--) 71 | { 72 | cin>>g; 73 | if(bridge[edges[g]]==1) 74 | { 75 | cout<<"Unhappy"< 2 | 3 | using namespace std; 4 | 5 | const int mx= 1e6+5; 6 | 7 | int parent[mx]; 8 | bool vis[mx]; 9 | int disc[mx]; 10 | int low[mx]; 11 | map bridge; 12 | vector>adj[mx]; 13 | 14 | void apufind(int u) 15 | { 16 | static int time =0; 17 | disc[u]=low[u]=time++; 18 | vis[u]=true; 19 | for(int i=0;i<(int)adj[u].size();i++) 20 | { 21 | int v=adj[u][i].first; 22 | int id=adj[u][i].second; 23 | if(!vis[v]) 24 | { 25 | parent[v]=u; 26 | apufind(v); 27 | low[u]=min(low[u],low[v]); 28 | 29 | if(low[v]>disc[u]) 30 | { 31 | bridge[id]=1; 32 | } 33 | } 34 | else if( v!=parent[u]) 35 | { 36 | low[u]=min(disc[v],low[u]); 37 | } 38 | } 39 | } 40 | 41 | int main() 42 | 43 | { 44 | int n,m,id,k; 45 | scanf("%d%d%d",&n,&m,&k); 46 | int u,v; 47 | int i=1; 48 | while(m--) 49 | { 50 | scanf("%d%d%d",&u,&v,&id); 51 | adj[u].push_back(make_pair(v,id)); 52 | adj[v].push_back(make_pair(u,id)); 53 | } 54 | for(int i=1;i<=n;i++) 55 | { 56 | parent[i]=-1; 57 | vis[i]=false; 58 | } 59 | for(int i=1;i<=n;i++) 60 | { 61 | if(!vis[i]) 62 | { 63 | apufind(i); 64 | } 65 | } 66 | int p,g; 67 | while(k--) 68 | { 69 | scanf("%d",&g); 70 | if(bridge[g]==0) 71 | { 72 | printf("no\n"); 73 | } 74 | else 75 | { 76 | printf("YES\n"); 77 | } 78 | } 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /HackerEarth- Space smugglers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int mx=1e5+2; 6 | 7 | typedef long long int ll; 8 | 9 | typedef pair< pair , pair > jjj; 10 | 11 | typedef pair pii; 12 | 13 | vectoradj[mx]; 14 | 15 | vectorrev_adj[mx]; 16 | 17 | ll n; 18 | 19 | 20 | void dijkstra(ll source,ll *dist,bool*vis,vectoradj[]) 21 | 22 | { 23 | for(int i=0;i<=n;i++) 24 | { 25 | vis[i]=false; 26 | dist[i]=LONG_MAX; 27 | } 28 | priority_queue,greater >pq; 29 | pq.push(make_pair(0,source)); 30 | dist[source]=0; 31 | while(!pq.empty()) 32 | { 33 | pii p=pq.top(); 34 | pq.pop(); 35 | int u =(int) p.second; 36 | if(vis[u]) continue; 37 | 38 | vis[u]=true; 39 | 40 | for(int i=0;idist[u]+weight) 46 | { 47 | dist[v]=dist[u]+weight; 48 | pq.push(make_pair(dist[v],v)); 49 | } 50 | } 51 | } 52 | 53 | } 54 | 55 | int main() 56 | 57 | { 58 | ll m; 59 | ll s,t,u,v,g; 60 | cin>>n>>m>>s>>t; 61 | while(m--) 62 | { 63 | cin>>u>>v>>g; 64 | adj[u].push_back(make_pair(v,g)); 65 | rev_adj[v].push_back(make_pair(u,g)); 66 | } 67 | bool vis[n+1]; 68 | 69 | ll dist_s_i[n+1]; 70 | ll dist_i_t[n+1]; 71 | ll dist_t_i[n+1]; 72 | ll dist_i_s[n+1]; 73 | 74 | dijkstra(s,dist_s_i,vis,adj); 75 | dijkstra(t,dist_i_t,vis,rev_adj); 76 | dijkstra(t,dist_t_i,vis,adj); 77 | dijkstra(s,dist_i_s,vis,rev_adj); 78 | 79 | ll ans=LONG_MAX; 80 | 81 | ll vertex; 82 | 83 | for(int i=1;i<=n;i++) 84 | 85 | { 86 | if(dist_s_i[i]==LONG_MAX || dist_i_t[i]==LONG_MAX || dist_t_i[i]==LONG_MAX || dist_i_s[i]==LONG_MAX||i==s||i==t) 87 | { 88 | continue; 89 | } 90 | if(dist_s_i[i]+dist_i_t[i]+dist_t_i[i]+dist_i_s[i] 2 | 3 | using namespace std; 4 | 5 | const int mx=1e3+1; 6 | 7 | typedef pair pi; 8 | 9 | bool visited[mx][mx]; 10 | int dis[mx][mx]; 11 | int matrix[mx][mx]; 12 | int n,m; 13 | 14 | vector vec={{0,1},{0,-1},{1,0},{-1,0}}; 15 | 16 | bool isvalid(int a,int b) 17 | { 18 | if(a>=0 && a<=n && b>=0 && b<=m && matrix[a][b] == 1) 19 | return true; 20 | 21 | return false; 22 | } 23 | 24 | void solution() 25 | { 26 | int x,y,xx,yy; 27 | queueq; 28 | for(int i=0;i>n>>m; 66 | for(int i=0;i>matrix[i][j]; 71 | } 72 | } 73 | memset(visited,false,sizeof(visited)); 74 | solution(); 75 | int mx=0; 76 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | 6 | vectoradj[13]; 7 | bool visited[12]={false};// visited or not ?? 8 | int disc[12]={0};// dfs time finding 9 | int low[12]={0};// low time for finding cycle 10 | int parent[12]={-1};// -1 holds for Null 11 | bool apu[12]={false};// which vertices are articular point ? 12 | vector< pair >bridges; // push back the bridges into a vector 13 | int articupoints=0; 14 | 15 | void finding(int u) 16 | { 17 | static int time=0; 18 | int child=0; 19 | visited[u]=true; 20 | disc[u]=low[u]=time++; 21 | vector::iterator it; 22 | for(it=adj[u].begin();it!=adj[u].end();it++) 23 | { 24 | int v=(*it); 25 | if(!visited[v]) 26 | { 27 | child++; 28 | parent[v]=u; 29 | finding(v); 30 | 31 | low[u]=min(low[u],low[v]); 32 | if(parent[u]==-1 && child>1) 33 | { 34 | articupoints++; 35 | apu[u]=true;// finding root with different independent subtree 36 | } 37 | if(parent[u]!=-1 && low[v]>=disc[u]) // there is no way to reach v without covereing u 38 | { 39 | articupoints++; 40 | apu[u]=true; 41 | } 42 | if(low[v]>disc[u]) 43 | { 44 | bridges.push_back(make_pair(min(v,u),max(v,u)));// storing bridges pair 45 | } 46 | 47 | } 48 | else if(v!=parent[u]) 49 | { 50 | low[u]=min(low[u],disc[v]); 51 | } 52 | } 53 | 54 | } 55 | 56 | bool comp(pair&a,pair&b) 57 | { 58 | if(a.first==b.first) 59 | { 60 | return (a.second>N>>M; 73 | while(M--) 74 | { 75 | cin>>u>>v; 76 | adj[u].push_back(v); 77 | adj[v].push_back(u); 78 | } 79 | for(int i=0;i<12;i++) 80 | { 81 | if(!visited[i]) 82 | { 83 | finding(i); 84 | } 85 | } 86 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n,desti,u,u1; 8 | cin>>n; 9 | int m=n-1; 10 | vectorv[n+1]; 11 | while(m--) 12 | { 13 | cin>>u>>u1; 14 | v[u].push_back(u1); 15 | v[u1].push_back(u); 16 | } 17 | cin>>desti; 18 | bool visited[n+1]; 19 | memset(visited,false,sizeof(visited)); 20 | int dist[n+1]={0}; 21 | queueq; 22 | dist[1]=1; 23 | q.push(1); 24 | visited[1]=true; 25 | while(q.empty()==false) 26 | { 27 | int temp=q.front(); 28 | q.pop(); 29 | for(int i=0;i 2 | 3 | using namespace std; 4 | const int mx=1e5+1; 5 | bool visited[mx]; 6 | void dfs(int u,vectorv[]) 7 | { 8 | visited[u]=true; 9 | vector::iterator it; 10 | for(it=v[u].begin();it!=v[u].end();it++) 11 | { 12 | if(!visited[(*it)]) 13 | { 14 | dfs((*it),v); 15 | } 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | int n,m,u,t,head; 22 | cin>>n>>m; 23 | vectorv[n+1]; 24 | for(int i=0;i>u>>t; 27 | v[u].push_back(t); 28 | v[t].push_back(u); 29 | } 30 | cin>>head; 31 | memset(visited,false,sizeof(visited)); 32 | dfs(head,v); 33 | int count=0; 34 | for(int i=1;i<=n;i++) 35 | { 36 | if(!visited[i]) 37 | { 38 | count++; 39 | } 40 | } 41 | cout< 2 | 3 | const long mod=100000; 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | long n,key,num,a; 10 | cin>>n>>key; 11 | cin>>num; 12 | vectorkeylist; 13 | while(num--) 14 | { 15 | cin>>a; 16 | keylist.push_back(a); 17 | } 18 | queueq; 19 | q.push(n); 20 | long count[100001]; 21 | for(int i=0;i<=100000;i++) 22 | { 23 | count[i]=-1;// nonvisited means -1,it's not possible to make i 24 | } 25 | count[n]=0; 26 | while(!q.empty()) 27 | { 28 | long nw=q.front(); 29 | if(key==nw) 30 | { 31 | break; // done , we have found 32 | } 33 | q.pop(); 34 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | const int mx=11; 6 | int mat[mx][mx]; 7 | int n,m; 8 | 9 | void dfs(int i,int j) 10 | { 11 | if(i<0 || j<0 || i>=n || j>=m || mat[i][j]==0) return ; 12 | 13 | mat[i][j]=0;// when reaching here , this index becomes zero 14 | 15 | dfs(i+1,j); 16 | dfs(i-1,j); 17 | dfs(i,j+1); 18 | dfs(i,j-1); 19 | } 20 | 21 | int main() 22 | 23 | { 24 | cin>>n>>m; 25 | for(int i=0;i>mat[i][j]; 30 | } 31 | } 32 | 33 | dfs(0,0); 34 | 35 | if(mat[n-1][m-1]==0) 36 | { 37 | cout<<"Yes"< 2 | 3 | using namespace std; 4 | 5 | bool visited[100001]; 6 | vectorv[100001]; 7 | int child[100001]; 8 | int parent[100001]; 9 | sethappy; 10 | 11 | void bfs(int s) 12 | { 13 | visited[s]=true; 14 | queueq; 15 | q.push(s); 16 | visited[s]=true; 17 | while(!q.empty()){ 18 | int nw=q.front(); 19 | q.pop(); 20 | vector::iterator it; 21 | for(it=v[nw].begin();it!=v[nw].end();it++) 22 | { 23 | if(!visited[(*it)]) 24 | { 25 | visited[(*it)]=true; 26 | q.push((*it)); 27 | child[nw]++; 28 | parent[(*it)]=nw; 29 | } 30 | } 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | int n,m,x,y; 37 | cin>>n>>m; 38 | if(m==0) 39 | { 40 | cout<<"0"<>x>>y; 46 | v[x].push_back(y); 47 | v[y].push_back(x); 48 | } 49 | memset(visited,false,sizeof(visited)); 50 | for(int i=0;i<100001;i++) 51 | { 52 | child[i]=0; 53 | parent[i]=i; 54 | } 55 | for(int i=1;i<=n;i++) 56 | { 57 | if(!visited[i]) 58 | { 59 | bfs(i); 60 | } 61 | } 62 | int answer=0; 63 | for(int i=1;i<=n;i++) 64 | { 65 | if(child[parent[i]] 2 | 3 | using namespace std; 4 | 5 | const int MAX = 1e5+5; 6 | 7 | typedef pairP; 8 | priority_queue,greater

>pq; 9 | 10 | bool visited[MAX]={false}; 11 | vector

adj[MAX]; 12 | long minimum_cost=0; 13 | 14 | void prim(int src) 15 | { 16 | pq.push(make_pair(0,src)); 17 | minimum_cost=0; 18 | P pr; 19 | while(!pq.empty()) 20 | { 21 | int u=pq.top().second; 22 | pr=pq.top(); 23 | pq.pop(); 24 | 25 | if(visited[u]==false) visited[u]=true; 26 | 27 | else 28 | continue; 29 | 30 | minimum_cost+=pr.first; 31 | 32 | vector

::iterator it; 33 | for(it=adj[u].begin();it!=adj[u].end();it++) 34 | { 35 | int v= (*it).first; 36 | int weight=(*it).second; 37 | if(visited[v]==false) 38 | { 39 | pq.push(make_pair(weight,v)); 40 | } 41 | } 42 | } 43 | } 44 | 45 | int main() 46 | 47 | { 48 | int N,M,u,v,w; 49 | cin>>N>>M; 50 | while(M--) 51 | { 52 | cin>>u>>v>>w; 53 | adj[u].push_back(make_pair(v,w)); 54 | adj[v].push_back(make_pair(u,w)); 55 | } 56 | prim(1); 57 | cout< 2 | 3 | using namespace std; 4 | 5 | const int mx=1e6+1; 6 | 7 | #define inf INT_MAX; 8 | 9 | typedef pairpii; 10 | vectoradj[mx]; 11 | int n,m; 12 | 13 | 14 | void bfs(int source,int *dis) 15 | { 16 | queueq; 17 | for(int i=1;i<=n;i++) 18 | { 19 | dis[i]=inf; 20 | } 21 | dis[source]=0; 22 | q.push(source); 23 | while(!q.empty()) 24 | { 25 | int u=q.front(); 26 | q.pop(); 27 | vector::iterator it; 28 | for(it=adj[u].begin();it!=adj[u].end();it++) 29 | { 30 | if(dis[(*it)]>dis[(u)]+1) // minimum level finding ,that's why we are not using any visited array 31 | { 32 | dis[(*it)]=dis[(u)]+1; 33 | q.push((*it)); 34 | } 35 | } 36 | } 37 | } 38 | 39 | int main() 40 | 41 | { 42 | int u,v,s,d; 43 | cin>>n>>m; 44 | while(m--) 45 | { 46 | cin>>u>>v; 47 | adj[u].push_back(v); // bidirectional graph 48 | adj[v].push_back(u); 49 | } 50 | cin>>s>>d; 51 | int source_to_dest[n+1]; 52 | int dest_to_source[n+1]; 53 | 54 | bfs(s,source_to_dest);// bfs for assuming source as a source 55 | bfs(d,dest_to_source);// bfs for assuming destination as a source 56 | 57 | vectorlevel[n]; 58 | for(int i=1;i<=n;i++) 59 | { 60 | if(i!=s && i!=d) 61 | { 62 | if(source_to_dest[i]+dest_to_source[i]==source_to_dest[d]) 63 | { 64 | level[source_to_dest[i]].push_back(i);// That means this node is in the shortest path way 65 | } 66 | } 67 | } 68 | 69 | int ans=INT_MAX; 70 | for(int i=0;i<=n;i++) 71 | { 72 | if(level[i].size() == 1) // when there is no other vertex in this level as a shortest path vertex then cutting this the path will be increased 73 | { 74 | ans=min(ans,level[i][0]); 75 | } 76 | } 77 | 78 | if(ans==INT_MAX) // there is many shortest path ,then by cutting one vertex doesn't affect the shortest path 79 | cout<<"-1"< 4 | using namespace std; 5 | #define mod 1000000007 6 | #define ll long long int 7 | #define pb push_back 8 | #define mk make_pair 9 | #define pp pair,pair > 10 | vector >adj[100002]; 11 | pairdis[100002]; 12 | priority_queue,greater >q; 13 | int main() 14 | { 15 | ios_base::sync_with_stdio(0); cin.tie(0); 16 | ll n,m,u,v,x,w; 17 | cin>>n>>m; 18 | while(m--) { 19 | cin>>u>>v>>w; 20 | adj[u].pb({v,w}); 21 | adj[v].pb({u,w}); 22 | } 23 | for(ll i = 1; i <= n; i++) { 24 | dis[i].first = dis[i].second = 1000000000000LL; 25 | } 26 | pp p; 27 | p.first = {0,0}; 28 | p.second = {1,-1}; 29 | q.push(p); 30 | dis[1].first = dis[1].second = 0; 31 | while(q.size()) { 32 | p = q.top(); 33 | q.pop(); 34 | u = p.second.first; 35 | for(ll i = 0; i < adj[u].size(); i++) { 36 | v = adj[u][i].first; 37 | w = adj[u][i].second; 38 | x = p.first.second + ((p.second.second == -1 || p.second.second == w%2)?0:1); 39 | w += p.first.first; 40 | pairp1 = dis[v]; 41 | if(p1.first < w) continue; 42 | if(p1.first == w && p1.second < x) continue; 43 | dis[v] = {w,x}; 44 | q.push({{w,x},{v,(adj[u][i].second)%2}}); 45 | } 46 | } 47 | cout< 2 | 3 | using namespace std; 4 | 5 | typedef pairP; 6 | priority_queue,greater

>pq; 7 | #define inf 1e7 8 | vectordist(10001,inf); 9 | 10 | bool visited[100000]={false}; 11 | vector

adj[10001]; 12 | 13 | void short_path(int src,int V) 14 | { 15 | pq.push(make_pair(0,src)); 16 | dist[src]=0; 17 | while(!pq.empty()) 18 | { 19 | int u=pq.top().second; 20 | pq.pop(); 21 | 22 | if(visited[u]==false) visited[u]=true; 23 | 24 | else 25 | continue; 26 | 27 | vector

::iterator it; 28 | for(it=adj[u].begin();it!=adj[u].end();it++) 29 | { 30 | int v=(*it).first; 31 | int weight= (*it).second; 32 | 33 | if(dist[v]>dist[u]+weight) 34 | { 35 | dist[v]=dist[u]+weight; 36 | pq.push(make_pair(dist[v],v)); 37 | } 38 | } 39 | } 40 | } 41 | 42 | int main() 43 | 44 | { 45 | int N,M,u,v,w; 46 | cin>>N>>M; 47 | while(M--) 48 | { 49 | cin>>u>>v>>w; 50 | adj[u].push_back(make_pair(v,w)); 51 | } 52 | short_path(1,N); 53 | int count=0; 54 | for(int i=2;i<=N;i++) 55 | { 56 | count++; 57 | if(count==1) 58 | { 59 | if(dist[i]==inf) 60 | cout<<"1000000000"; 61 | else 62 | cout< 13 | 14 | using namespace std; 15 | 16 | typedef pairpii; 17 | const int mx=1e6; 18 | bool vis[mx]; 19 | int parent[mx]; 20 | vectoradj[mx]; 21 | int dist[mx]; 22 | 23 | void dijkstra(int source) 24 | { 25 | priority_queueq; 26 | dist[source]=1000000007; 27 | parent[source]=source; 28 | q.push(make_pair(dist[source],source)); 29 | while(!q.empty()) 30 | { 31 | pii pq=q.top(); 32 | q.pop(); 33 | int u=pq.second; 34 | if(vis[u]) 35 | { 36 | continue; 37 | } 38 | vis[u]=true; 39 | vector::iterator it; 40 | for(it=adj[u].begin();it!=adj[u].end();it++) 41 | { 42 | int v=(*it).second; 43 | int capacity=(*it).first; 44 | int lwmn=min(dist[u],capacity); 45 | if(lwmn>dist[v]) 46 | { 47 | parent[v]=u; 48 | dist[v]=lwmn; 49 | q.push(make_pair(dist[v],v)); 50 | } 51 | } 52 | } 53 | } 54 | 55 | int main() 56 | 57 | { 58 | int n,m,u,v,w; 59 | cin>>n>>m; 60 | for(int i=0;i<=n;i++) 61 | { 62 | parent[i]=-1; 63 | dist[i]=0; 64 | vis[i]=false; 65 | } 66 | int s,d,c; 67 | while(m--) 68 | { 69 | cin>>u>>v>>w; 70 | adj[u].push_back(make_pair(w,v)); 71 | adj[v].push_back(make_pair(w,u)); 72 | } 73 | cin>>s>>d>>c; 74 | dijkstra(s); 75 | vectorans; 76 | ans.push_back(d); 77 | int t=d; 78 | while(t!=s) 79 | { 80 | t=parent[t]; 81 | ans.push_back(t); 82 | } 83 | for(int j=ans.size()-1;j>=0;j--) 84 | { 85 | cout< 2 | 3 | using namespace std; 4 | 5 | void dfs(int u,bool visited[],vectorV[]) 6 | { 7 | visited[u]=true; 8 | vector::iterator it; 9 | for(it=V[u].begin();it!=V[u].end();it++) 10 | { 11 | if(!visited[*it]) 12 | { 13 | dfs((*it),visited,V); 14 | } 15 | } 16 | } 17 | 18 | int main() 19 | 20 | { 21 | int n,m,k,u,v; 22 | cin>>n>>m>>k; 23 | vectorV[n+1]; 24 | int sz=0; 25 | for(int i=0;i>u>>v; 28 | V[u].push_back(v); 29 | V[v].push_back(u); 30 | } 31 | bool * visited=new bool [n+1]; 32 | memset(visited,false,sizeof(visited)); 33 | int num_of_connected=0; 34 | for(int i=1;i<=n;i++) 35 | { 36 | if(visited[i]==false) 37 | { 38 | dfs(i,visited,V); 39 | //cout<k) 44 | { 45 | cout<<"-1"< 2 | 3 | using namespace std; 4 | int c=0,d=0; 5 | int bar=0; 6 | vectorv[17]; 7 | vectorrev[17]; 8 | stacks; 9 | void dfs(int u,bool visited[]) 10 | { 11 | visited[u]=true; 12 | vector::iterator it; 13 | for( it=v[u].begin();it!=v[u].end();it++) 14 | { 15 | if(!visited[*it]) 16 | { 17 | dfs((*it),visited); 18 | } 19 | } 20 | //cout<::iterator it; 29 | for( it=rev[u].begin();it!=rev[u].end();it++) 30 | { 31 | if(!nwvisited[*it]) 32 | { 33 | dfs2((*it),nwvisited); 34 | } 35 | } 36 | } 37 | int main() 38 | 39 | { 40 | int m; 41 | int n,u,t; 42 | cin>>n>>m; 43 | for(int i=0;i>u>>t; 46 | v[u].push_back(t); 47 | rev[t].push_back(u); 48 | } 49 | bool *visited=new bool[n+1]; 50 | for(int i=1;i<=n;i++) 51 | { 52 | visited[i]=false; 53 | } 54 | for(int i=1;i<=n;i++) 55 | { 56 | bar=1; 57 | if(!visited[i]){ 58 | dfs(i,visited);} 59 | //bar++; 60 | } 61 | bool *nwvisited=new bool[n+1]; 62 | for(int i=1;i<=n;i++) 63 | { 64 | nwvisited[i]=false; 65 | } 66 | while(!s.empty()) 67 | { 68 | int b=s.top(); 69 | //cout<<"stack"< 2 | 3 | using namespace std; 4 | 5 | const int mx=1e6+1; 6 | 7 | typedef pairpii; 8 | 9 | bool vis[mx]={false}; 10 | long dist[mx]; 11 | vectoradj[mx]; 12 | priority_queue,greater > pq; 13 | 14 | void dijkstra() 15 | { 16 | pq.push(make_pair(0,1)); 17 | dist[1]=0; 18 | while(!pq.empty()) 19 | { 20 | int u=pq.top().second; 21 | pq.pop(); 22 | if(vis[u]) 23 | continue; 24 | 25 | vis[u]=true; 26 | vector::iterator it; 27 | for(it=adj[u].begin();it!=adj[u].end();it++) 28 | { 29 | int v=(*it).first; 30 | int weight =(*it).second; 31 | //cout<dist[u]+weight)) 33 | { 34 | dist[v]=dist[u]+weight; 35 | pq.push(make_pair(dist[v],v)); 36 | } 37 | } 38 | } 39 | } 40 | 41 | 42 | int main() 43 | { 44 | int n,m,f; 45 | cin>>n>>m>>f; 46 | int cap[n+1]; 47 | for(int i=1;i<=n;i++) 48 | { 49 | cin>>cap[i]; 50 | } 51 | int u,v,w; 52 | while(m--) 53 | { 54 | cin>>u>>v>>w; 55 | adj[u].push_back(make_pair(v,w)); 56 | adj[v].push_back(make_pair(u,w)); 57 | } 58 | int x; 59 | for(int i=1;i<=n;i++) 60 | { 61 | 62 | dist[i]=LONG_MAX; 63 | } 64 | dijkstra(); 65 | vectorvec; 66 | for(int l=1;l<=n;l++) 67 | { 68 | if(dist[l]!=LONG_MAX){ 69 | vec.push_back(make_pair(dist[l]+f,cap[l]));} // cost , capacity 70 | } 71 | sort(vec.begin(),vec.end()); 72 | vec.push_back(make_pair(-1,10000000)); 73 | int i=0; 74 | int k; 75 | cin>>k; 76 | while(k>vec[i].second) 77 | { 78 | for(int h=0;h 2 | 3 | using namespace std; 4 | 5 | bool visited[100]={false}; 6 | vectoradj[25]; 7 | stacks; 8 | 9 | void dfs(int u) 10 | { 11 | visited[u]=true; 12 | for(int i=0;i>V>>e; 27 | for(int i=0;i>u>>v; 30 | adj[u].push_back(v); 31 | } 32 | for(int j=0;j<25;j++) 33 | { 34 | int sz=adj[j].size(); 35 | if(sz>0) 36 | { 37 | sort(adj[j].begin(),adj[j].end(),greater());// decreasing order 38 | } 39 | } 40 | for(int k=V;k>0;k--) 41 | { 42 | if(!visited[k]) 43 | { 44 | dfs(k); 45 | } 46 | } 47 | int count=0; 48 | while(!s.empty()) 49 | { 50 | int val=s.top(); 51 | s.pop(); 52 | count++; 53 | if(count==1) 54 | { 55 | cout< u -> v-> e ) now total cost will be , 4 | when u will go from s to u , cost[s][u] and (for acknowledging ) cost of back from u to s cost[u][s] , 5 | for only (s->u) cost[s][u] + cost[u][s] !! Now for , ( u->v ) cost[u][v] + cost[v][u] !! Such as ...... 6 | cost[s][u] + cost[u][s] + cost[u][v] + cost[v][u] +cost[v][e]+cost[e][v] will be our answer . 7 | Answer should be minimized . We have to use floyd warshall algorithm to find shortest path of any pair . 8 | Then after doing this,make the cost of every graph[u][v] to graph[u][v] + dist[v][u],where dist has been found by floyd wrshall . 9 | Now each graph[u][v] holds the value of graph[u][v] itself and dist[v][u] summation ........... 10 | Now run again floyd warshall to find shortest path from source to destination ................... 11 | */ 12 | #include 13 | 14 | using namespace std; 15 | 16 | const int N=303; 17 | 18 | int graph[N][N]; 19 | int dist[N][N]; 20 | 21 | 22 | int main() 23 | 24 | { 25 | int n,m,u,v,source,destination,w; 26 | 27 | for(int i=0;i>n>>m>>source>>destination; 36 | 37 | while(m--) 38 | { 39 | cin>>u>>v>>w; 40 | graph[u][v]=dist[u][v]=w; 41 | } 42 | 43 | 44 | // Here we will use floyd warshall algo , because we need the shortest path of any two pairs for building our algo 45 | // floyd warshall algorithm to find the shortest path between any pairs { i,j } -> dist[i][j] 46 | 47 | for(int k=1;k<=n;k++) 48 | { 49 | for(int i=1;i<=n;i++) 50 | { 51 | for(int j=1;j<=n;j++) 52 | { 53 | if(dist[i][k]==INT_MAX||dist[k][j]==INT_MAX) 54 | continue; 55 | 56 | dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]); 57 | } 58 | } 59 | } 60 | 61 | // Now , the graph[i][j] should be the equal to graph[i][j] + dist[j][i] 62 | 63 | for(int i=1;i<=n;i++) 64 | { 65 | for(int j=1;j<=n;j++) 66 | { 67 | if(graph[i][j]!=INT_MAX && dist[j][i]!=INT_MAX) 68 | graph[i][j]=graph[i][j]+dist[j][i]; 69 | 70 | else 71 | graph[i][j]=INT_MAX; // if there is no way back from j to i dist[j][i]=inf then also graph[i][j] inf 72 | } 73 | } 74 | 75 | // Now , the value of graph edges graph[i][j] has been changed ,nw finding the shortest distance from source to destination ,I will use floyd warshall because of adjacent matrix 76 | 77 | for(int k=1;k<=n;k++) 78 | { 79 | for(int i=1;i<=n;i++) 80 | { 81 | for(int j=1;j<=n;j++) 82 | { 83 | if(graph[i][k]==INT_MAX || graph[k][j]==INT_MAX) 84 | continue; 85 | 86 | graph[i][j]=min(graph[i][j],graph[i][k]+graph[k][j]); 87 | } 88 | } 89 | } 90 | 91 | cout<<(graph[source][destination]==INT_MAX?-1:graph[source][destination])< 7 | 8 | using namespace std; 9 | 10 | bool myfunc(pair &a,pair &b) 11 | { 12 | if(a.second==b.second) 13 | return a.first>b.first; 14 | return a.second>b.second; 15 | } 16 | 17 | int main() 18 | 19 | { 20 | int n,m1,k,val,u,v; 21 | cin>>n>>m1>>k; 22 | vectoradj[n+1]; 23 | mapm; 24 | mapva; 25 | int count=1; 26 | for(int j=1;j<=n;j++) 27 | { 28 | cin>>val; 29 | m[j]=val; 30 | va[val]=j; 31 | } 32 | while(m1--) 33 | { 34 | cin>>u>>v; 35 | adj[u].push_back(v); 36 | adj[v].push_back(u); 37 | } 38 | for(int i=1;i<=n;i++) 39 | { 40 | vector > vec; 41 | if(adj[i].size()ans) 9 | { 10 | ans=lev; 11 | } 12 | if(root->left!=NULL) 13 | { 14 | max(root->left,lev+1); 15 | } 16 | if(root->right!=NULL) 17 | { 18 | max(root->right,lev+1); 19 | } 20 | } 21 | int maxDepth(TreeNode* root) { 22 | if(root==NULL) 23 | return 0; 24 | 25 | max(root,0); 26 | return ans+1; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Light oj 1009 - Back to Underworld.cpp: -------------------------------------------------------------------------------- 1 | 2 | /// Time - 0.345s 3 | /// Light oj 1009 - Back to Underworld 4 | /// Graph theory + Bipartite. 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | const int maxn=20001; 11 | 12 | const int BLACK=1; 13 | const int RED=2; 14 | const int WHITE=0; 15 | 16 | vectoradj[maxn]; 17 | int color[maxn]; 18 | int vampire=0; 19 | int lykan=0; 20 | int ans=0; 21 | 22 | void clear_adj() 23 | { 24 | for(int i=0;iq; 30 | color[s]=BLACK; /// consider it as vampire and color it as BLACK 31 | vampire++; /// as first one is evaluated 32 | q.push(s); 33 | 34 | while(!q.empty()) 35 | { 36 | int u=q.front(); 37 | q.pop(); 38 | 39 | for(int i=0;i>T; 67 | for(int caso=1;caso<=T;caso++) 68 | { 69 | clear_adj(); 70 | memset(color,0,sizeof(color)); 71 | 72 | vampire=0; 73 | lykan=0; 74 | ans=0; 75 | 76 | cin>>n; 77 | 78 | while(n--) 79 | { 80 | cin>>u>>v; 81 | adj[u].push_back(v); 82 | adj[v].push_back(u); 83 | } 84 | 85 | for(int i=0;i 10 | 11 | using namespace std; 12 | 13 | int col,row; 14 | 15 | char grid[21][21]; 16 | int vis[21][21]; 17 | 18 | /// up - down - right - left 19 | int dx[]={0,0,1,-1}; 20 | int dy[]={1,-1,0,0}; 21 | 22 | int ans; 23 | 24 | 25 | void BFS(int src_x,int src_y) 26 | { 27 | queue >q; 28 | vis[src_x][src_y]=1; /// consider it as vampire and color it as BLACK 29 | q.push(make_pair(src_x,src_y)); 30 | 31 | while(!q.empty()) 32 | { 33 | int x=q.front().first; 34 | int y=q.front().second; 35 | q.pop(); 36 | 37 | for(int i=0;i<4;i++) 38 | { 39 | int xx=x+dx[i]; 40 | int yy=y+dy[i]; 41 | 42 | if(xx>=0 && yy>=0 && xx>T; 58 | for(int caso=1;caso<=T;caso++) 59 | { 60 | 61 | memset(vis,0,sizeof(vis)); 62 | ans=1; 63 | int src_x,src_y; 64 | cin>>row>>col; 65 | for(int i=0;i>grid[i][j]; 70 | 71 | if(grid[i][j]=='@') 72 | { 73 | src_x=i; 74 | src_y=j; 75 | } 76 | } 77 | } 78 | BFS(src_x,src_y); 79 | printf("Case %d: %d\n",caso,ans); 80 | 81 | } 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /LightOj 1049 - One Way Roads.cpp: -------------------------------------------------------------------------------- 1 | 2 | /// Time- 0.003s 3 | /// LightOj 1049 - One Way Roads 4 | /// Greedy Graph Theory. 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | const int mx=1001; 11 | 12 | int main() 13 | { 14 | int T; 15 | scanf("%d",&T); 16 | for(int caso=1;caso<=T;caso++) 17 | { 18 | int n,u,v,w; 19 | scanf("%d",&n); 20 | int left_side[n+1]; 21 | int right_side[n+1]; 22 | memset(left_side,0,sizeof(left_side)); 23 | memset(right_side,0,sizeof(right_side)); 24 | int straight=0; 25 | int reverses=0; 26 | for(int i=1;i<=n;i++) 27 | { 28 | scanf("%d%d%d",&u,&v,&w); 29 | /// it is only for one side left to right 30 | if(left_side[u]==0 && right_side[v]==0) 31 | { 32 | straight+=w; 33 | left_side[u]=right_side[v]=1; 34 | } 35 | /// it is only for reverse right to left 36 | else 37 | { 38 | right_side[u]=left_side[v]=1; 39 | reverses+=w; 40 | } 41 | } 42 | printf("Case %d: %d\n",caso,min(straight,reverses)); 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /LightOj 1141 - Number Transformation.cpp: -------------------------------------------------------------------------------- 1 | 2 | /// Time- 0.005s 3 | /// LightOj 1141 - Number Transformation 4 | /// BFS 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | int SRC, DEST; 11 | vector primef[1001]; 12 | void primefactor( int n) 13 | { 14 | int num = n; 15 | int fact = 2; 16 | int fct = 0 ; 17 | while(n > 1) 18 | { 19 | fct = 0; 20 | while( n % fact == 0 ) 21 | { 22 | fct=fact; 23 | n /= fact; 24 | } 25 | if(fct) primef[num].push_back(fct); 26 | fact += 1; 27 | } 28 | if( fct == num ) primef[num].clear(); 29 | } 30 | int bfs( ) 31 | { 32 | int dist[1001]; 33 | memset(dist, -1, sizeof(dist)); 34 | dist[SRC] = 0; 35 | queue q; 36 | q.push(SRC); 37 | int from, to; 38 | while( !q.empty() ){ 39 | from = q.front(); 40 | q.pop(); 41 | for( int i = 0; i < (int) primef[from].size(); i++ ){ 42 | to = from + primef[from][i]; 43 | if( to<=DEST && dist[to]==-1 ) 44 | { 45 | q.push(to); 46 | dist[to] = dist[from] + 1; 47 | if(to==DEST) return dist[DEST]; 48 | } 49 | } 50 | } 51 | return dist[DEST]; 52 | } 53 | 54 | int main(){ 55 | 56 | int T; 57 | int caso = 0; 58 | //freopen("input.txt", "r", stdin); 59 | for( int i = 2; i <= 1000; i++ ) primefactor(i); 60 | scanf("%d", &T); 61 | while( T-- ){ 62 | caso++; 63 | scanf("%d %d", &SRC, &DEST); 64 | printf("Case %d: %d\n", caso, bfs()); 65 | } 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /LightOj- 1066 - Gathering Food.cpp: -------------------------------------------------------------------------------- 1 | /// Time- 0.007s 2 | /// LightOj- 1066 - Gathering Food 3 | /// BFS + brute force 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | const int siz=13; 10 | 11 | int n; 12 | 13 | string grid[siz]; 14 | int dist[siz][siz]; 15 | bool vis[siz][siz]; 16 | char dest; 17 | int src_x,src_y; 18 | 19 | /// move side 20 | int dx[]={1,-1,0,0}; 21 | int dy[]={0,0,1,-1}; 22 | 23 | void initia() 24 | { 25 | for(int i=0;i>Q; 39 | Q.push(make_pair(srcx,srcy)); 40 | vis[srcx][srcy]=true; 41 | dist[srcx][srcy]=0; 42 | int srcxx,srcyy,destxx,destyy; 43 | while(!Q.empty()) 44 | { 45 | pair p=Q.front(); 46 | Q.pop(); 47 | srcxx=p.first; 48 | srcyy=p.second; 49 | 50 | if(grid[srcxx][srcyy]==dest) 51 | { 52 | /// get new source's x and y . 53 | src_x=srcxx; 54 | src_y=srcyy; 55 | 56 | return dist[srcxx][srcyy]; 57 | } 58 | 59 | for(int i=0;i<4;i++) 60 | { 61 | destxx=srcxx+dx[i]; 62 | destyy=srcyy+dy[i]; 63 | 64 | if(destxx>=0 && destxx=0 && destyydest && dest<='Z') || (grid[destxx][destyy]=='#') ) 67 | { 68 | continue; 69 | } 70 | 71 | vis[destxx][destyy]=true; 72 | dist[destxx][destyy]=dist[srcxx][srcyy]+1; 73 | Q.push(make_pair(destxx,destyy)); 74 | } 75 | } 76 | } 77 | return -1; 78 | } 79 | 80 | 81 | int main() 82 | 83 | { 84 | int T,w; 85 | cin>>T; 86 | for(int caso=1;caso<=T;caso++) 87 | { 88 | scanf("%d",&n); 89 | for(int i=0;i>grid[i]; 92 | } 93 | int cnt=0; 94 | for(int i=0;i 7 | 8 | using namespace std; 9 | 10 | typedef unsigned long long int ll; 11 | 12 | const int mx=30001; 13 | 14 | vector >v[mx]; 15 | bool vis[mx]; 16 | int dist1[mx]; 17 | int dist2[mx]; 18 | 19 | int n,a,b; 20 | 21 | int max_index (int u,int dist[]) 22 | { 23 | memset(vis,false,sizeof(vis)); 24 | memset(dist,0,sizeof(dist)); 25 | vis[u]=true; 26 | queueq; 27 | q.push(u); 28 | dist[u]=0; 29 | while(!q.empty()) 30 | { 31 | int u=q.front(); 32 | q.pop(); 33 | for(int i=0;i>T; 64 | for(int caso=1;caso<=T;caso++) 65 | { 66 | for(int i=0;i>n; 73 | for(int i=1;i<=n-1;i++) 74 | { 75 | cin>>a>>b>>w; 76 | v[a].push_back(make_pair(b,w)); 77 | v[b].push_back(make_pair(a,w)); 78 | } 79 | int mx_dstance_indx=max_index(0,dist1); 80 | int fnal_dstnce_indx=max_index(mx_dstance_indx,dist2); 81 | printf("Case %d: %d\n",caso,dist2[fnal_dstnce_indx]); 82 | } 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /LightOj- 1111 Best Picnic Ever.cpp: -------------------------------------------------------------------------------- 1 | 2 | /// Time- 0.016s 3 | /// LightOj- 1111 - Best Picnic Ever 4 | /// BFS 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | typedef unsigned long long int ll; 11 | 12 | const int mx=1001; 13 | const int inf=INT_MAX; 14 | 15 | int N; 16 | int cont[mx]; 17 | vectoradj[mx]; 18 | 19 | void initi () 20 | { 21 | for(int i=0;iq; 33 | q.push(src); 34 | vis[src]=true; 35 | cont[src]++; 36 | while(!q.empty()) 37 | { 38 | int u=q.front(); 39 | q.pop(); 40 | for(int i=0;i>T; 59 | for(int caso=1;caso<=T;caso++) 60 | { 61 | initi(); 62 | int K,M,u,v; 63 | scanf("%d%d%d",&K,&N,&M); 64 | int places[K]; 65 | for(int i=0;i 7 | 8 | using namespace std; 9 | 10 | typedef unsigned long long int ll; 11 | 12 | const int mx=30001; 13 | 14 | vector >v[mx]; 15 | bool vis[mx]; 16 | long long dist1[mx]; 17 | long long dist2[mx]; 18 | long long dist3[mx]; 19 | 20 | int n,a,b; 21 | 22 | int max_index (int u,long long dist[]) 23 | { 24 | memset(vis,false,sizeof(vis)); 25 | memset(dist,0,sizeof(dist)); 26 | vis[u]=true; 27 | queueq; 28 | q.push(u); 29 | dist[u]=0; 30 | while(!q.empty()) 31 | { 32 | int u=q.front(); 33 | q.pop(); 34 | for(int i=0;i>T; 65 | for(int caso=1;caso<=T;caso++) 66 | { 67 | for(int i=0;i>n; 74 | for(int i=1;i<=n-1;i++) 75 | { 76 | cin>>a>>b>>w; 77 | v[a].push_back(make_pair(b,w)); 78 | v[b].push_back(make_pair(a,w)); 79 | } 80 | int mx_dstance_indx=max_index(0,dist1); 81 | int fnal_dstnce_indx=max_index(mx_dstance_indx,dist2); 82 | int another=max_index(fnal_dstnce_indx,dist3); 83 | printf("Case %d:\n",caso); 84 | for(int i=0;i 9 | 10 | using namespace std; 11 | 12 | 13 | int n; 14 | const int sz=10001; 15 | 16 | vectoradj[sz]; 17 | 18 | bool vis[sz]; 19 | int money[sz]; 20 | int total=0; 21 | int people=0; 22 | 23 | int initia() 24 | { 25 | for(int i=0;i>money[i],sum+=money[i];} 54 | while(m--) 55 | { 56 | cin>>u>>v; 57 | u--;v--; 58 | adj[u].push_back(v); 59 | adj[v].push_back(u); 60 | } 61 | bool ok=true; 62 | 63 | /// It will no 64 | if(sum%n!=0) {printf("Case %d: ",caso);printf("No\n");} 65 | 66 | else 67 | { 68 | int average=sum/n; /// getting equal . 69 | 70 | for(int i=0;i 11 | 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | int n,a,N; 17 | int sum=0; 18 | cin>>n; 19 | N=n; 20 | while(n--) 21 | { 22 | cin>>a; 23 | sum=sum+a; 24 | } 25 | if((sum/2) == N-1) 26 | { 27 | cout<<"Yes"< 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int cities,test; 11 | cin>>test; 12 | while(test--){ 13 | cin>>cities; 14 | sets; 15 | int a,b; 16 | while(cities--) 17 | { 18 | cin>>a>>b; 19 | s.insert(a); 20 | s.insert(b); 21 | } 22 | cout< 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | typedef pair ipair; 8 | 9 | const int mx=50001; 10 | 11 | vectoradj[mx]; 12 | 13 | int n; 14 | 15 | ll dist[mx]={0}; 16 | 17 | int shortest_path(int src) 18 | { 19 | priority_queue,greater >pq; 20 | bool vis[n+1]={false}; 21 | pq.push(make_pair(0,src)); 22 | for(int y=1;y<=n;y++) dist[y]=1e9; 23 | dist[src]=0; 24 | while(!pq.empty()) 25 | { 26 | int u=pq.top().second; 27 | pq.pop(); 28 | if(!vis[u]) vis[u]=true; 29 | else continue; 30 | 31 | for(int i=0;i>t; 55 | while(t--){ 56 | cin>>n; 57 | for(int i=1;i<=n;i++) adj[i].clear(); 58 | for(int j=0;j>a>>b>>c; 61 | adj[a].push_back(make_pair(b,c)); 62 | adj[b].push_back(make_pair(a,c)); 63 | } 64 | int y=shortest_path(1); 65 | ll ans=dist[shortest_path(y)]; 66 | cout< 2 | 3 | using namespace std; 4 | 5 | typedef pair ipair; 6 | 7 | const int mx=1e4+1; 8 | 9 | vectoradj[mx]; 10 | 11 | mapm; 12 | 13 | vectordist(mx); 14 | 15 | #define inf 1e7 16 | 17 | int n; 18 | 19 | void shortest_path(int src) 20 | { 21 | priority_queue,greater >pq; 22 | bool vis[n+1]={false}; 23 | pq.push(make_pair(0,src)); 24 | dist[src]=0; 25 | while(!pq.empty()) 26 | { 27 | int u=pq.top().second; 28 | pq.pop(); 29 | if(!vis[u]) vis[u]=true; 30 | else continue; 31 | 32 | for(int i=0;i>tc; 51 | while(tc--) 52 | { 53 | cin>>n; 54 | for(int y=1;y<=n;y++) adj[y].clear(),dist[y]=inf; 55 | for(int i=1;i<=n;i++) 56 | { 57 | cin>>s; 58 | m[s]=i; 59 | cin>>x; 60 | while(x--) 61 | { 62 | cin>>b>>w; 63 | adj[i].push_back(make_pair(b,w)); 64 | } 65 | } 66 | cin>>M; 67 | while(M--) 68 | { 69 | cin>>s>>s1; 70 | int sr=m[s]; 71 | int dst=m[s1]; 72 | shortest_path(sr); 73 | cout< 2 | 3 | using namespace std; 4 | 5 | const int mx=1e6+1; 6 | 7 | vectoradj[mx]; 8 | int color[mx]; 9 | 10 | bool possible(int source) 11 | { 12 | queueq; 13 | q.push(source); 14 | color[source]=0; 15 | while(!q.empty()) 16 | { 17 | int u=q.front(); 18 | q.pop(); 19 | for(int i=0;i>tc; 46 | int r=1; 47 | while(tc--) 48 | { 49 | cin>>n>>m; 50 | for(int i=1;i<=n;i++) 51 | { 52 | color[i]=-1; 53 | adj[i].clear(); 54 | } 55 | while(m--) 56 | { 57 | cin>>a>>b; 58 | adj[a].push_back(b); 59 | adj[b].push_back(a); 60 | } 61 | cout<<"Scenario #"< 2 | 3 | using namespace std; 4 | 5 | const int mx=700; 6 | 7 | typedef pair pi; 8 | vectorans; 9 | 10 | vectoradj[mx]; 11 | int low[mx]; 12 | int disc[mx]; 13 | int parent[mx]; 14 | bool visited[mx]; 15 | int s,t; 16 | 17 | void apufind(int u) 18 | { 19 | static int time=0; 20 | int child=0; 21 | visited[u]=true; 22 | disc[u]=low[u]=++time; 23 | int it; 24 | for(it=0;itdisc[u]) {ans.push_back(make_pair(min(u,v),max(u,v)));} 34 | } 35 | else if(v!=parent[u]) 36 | { 37 | low[u]=min(low[u],disc[v]); 38 | } 39 | } 40 | } 41 | 42 | int main() 43 | 44 | { 45 | ios::sync_with_stdio(false); 46 | cin.tie(NULL); 47 | cout.tie(NULL); 48 | int tc,n,m,k,a,b,c; 49 | cin>>tc; 50 | for(int l=1;l<=tc;l++) 51 | { 52 | cout<>n>>m; 55 | s++,t++; 56 | for(int i=1;i<=n;i++) 57 | { 58 | adj[i].clear(); 59 | low[i]=0; 60 | disc[i]=0; 61 | parent[i]=-1; 62 | visited[i]=false; 63 | ans.clear(); 64 | } 65 | while(m--) 66 | { 67 | cin>>a>>b; 68 | adj[a].push_back(b); 69 | adj[b].push_back(a); 70 | } 71 | apufind(1); 72 | 73 | if(ans.size()==0) 74 | cout<<"Sin bloqueos"< 2 | 3 | using namespace std; 4 | 5 | const int mx=129; 6 | typedef unsigned long long int ll; 7 | 8 | ll resgraph[mx][mx]; 9 | int parent[mx]; 10 | 11 | ll mx_flow=0; 12 | 13 | bool bfs(int s,int t) 14 | { 15 | bool visited[mx]={false}; 16 | queueq; 17 | q.push(s); 18 | visited[s]=true; 19 | parent[s]=-1; 20 | while(!q.empty()) 21 | { 22 | int u=q.front(); 23 | q.pop(); 24 | for(int v=0;v<129;v++) 25 | { 26 | if(visited[v]==false && resgraph[u][v]>0) 27 | { 28 | q.push(v); 29 | parent[v]=u; 30 | visited[v]=true; 31 | } 32 | } 33 | } 34 | return (visited[t]==true); 35 | } 36 | 37 | void fordFulker(int s,int t) 38 | { 39 | int v; 40 | while(bfs(s,t)==true) 41 | { 42 | ll path_flow=INT_MAX; 43 | for(v=t;v!=s;v=parent[v]) 44 | { 45 | int u=parent[v]; 46 | path_flow=min(path_flow,resgraph[u][v]); 47 | } 48 | for(v=t;v!=s;v=parent[v]) 49 | { 50 | int u=parent[v]; 51 | resgraph[u][v]-=path_flow; 52 | resgraph[v][u]+=path_flow; 53 | } 54 | //cout<>n; 70 | while(n--) 71 | { 72 | cin>>a>>b>>c; 73 | resgraph[(int)a][(int)b]+=c; 74 | } 75 | char s,t; 76 | s='A'; 77 | t='Z'; 78 | fordFulker((int)s,(int)t); 79 | cout< 2 | 3 | using namespace std; 4 | 5 | const int mx=206; 6 | typedef unsigned long long int ll; 7 | 8 | ll resgraph[mx][mx]; 9 | int parent[mx]; 10 | 11 | ll mx_flow=0; 12 | int n; 13 | 14 | bool bfs(int s,int t) 15 | { 16 | bool visited[mx]={false}; 17 | queueq; 18 | q.push(s); 19 | visited[s]=true; 20 | parent[s]=-1; 21 | while(!q.empty()) 22 | { 23 | int u=q.front(); 24 | q.pop(); 25 | for(int v=1;v<=n;v++) 26 | { 27 | if(visited[v]==false && resgraph[u][v]>0) 28 | { 29 | q.push(v); 30 | parent[v]=u; 31 | visited[v]=true; 32 | } 33 | } 34 | } 35 | return (visited[t]==true); 36 | } 37 | 38 | void fordFulker(int s,int t) 39 | { 40 | int v; 41 | while(bfs(s,t)==true) 42 | { 43 | ll path_flow=INT_MAX; 44 | for(v=t;v!=s;v=parent[v]) 45 | { 46 | int u=parent[v]; 47 | path_flow=min(path_flow,resgraph[u][v]); 48 | } 49 | for(v=t;v!=s;v=parent[v]) 50 | { 51 | int u=parent[v]; 52 | resgraph[u][v]-=path_flow; 53 | resgraph[v][u]+=path_flow; 54 | } 55 | //cout<>t; 68 | while(t--){ 69 | memset(resgraph,0,sizeof(resgraph)); 70 | memset(parent,0,sizeof(parent)); 71 | mx_flow=0; 72 | int m,v,u; 73 | ll c; 74 | cin>>n; 75 | for(int i=1;i<=n-1;i++) 76 | { 77 | 78 | cin>>a; 79 | for(int j=1;j<=a;j++) 80 | { 81 | cin>>c; 82 | if(i==1||c==n) resgraph[i][c]=1;//Only coming rout from 1 and entering to n should be different that's why 1 83 | else resgraph[i][c]=10000;// Other all routes can be used by more than 1 , so this is infinity say any big number 84 | } 85 | } 86 | fordFulker(1,n); 87 | cout< 2 | 3 | using namespace std; 4 | 5 | typedef unsigned long long int ll; 6 | 7 | const int mx=1e4+1; 8 | 9 | vectorv[mx]; 10 | int cont=0; 11 | bool vis1[mx]; 12 | bool vis2[mx]; 13 | 14 | bool cyclic(int u,int parent,bool vis[]) 15 | { 16 | vis[u]=true; 17 | for(int i=0;i>n>>m; 56 | while(m--) 57 | { 58 | cin>>a>>b; 59 | v[a].push_back(b); 60 | v[b].push_back(a); 61 | } 62 | bool cycle=false; 63 | for(int i=1;i<=n;i++) 64 | { 65 | if(!vis1[i]) 66 | { 67 | if(cyclic(i,-1,vis1)) 68 | { 69 | cycle=true; 70 | } 71 | } 72 | } 73 | dfs(1,vis2); 74 | bool disconnected=false; 75 | for(int i=1;i<=n;i++) 76 | { 77 | if(vis2[i]==false) 78 | { 79 | disconnected=true; 80 | } 81 | } 82 | if(cycle||disconnected) 83 | { 84 | cout<<"NO"< 2 | 3 | using namespace std; 4 | 5 | typedef unsigned long long int ll; 6 | 7 | const int mx=1e4+1; 8 | 9 | vectorv[mx]; 10 | int cont=0; 11 | bool vis[mx]; 12 | int dist1[mx]; 13 | int dist2[mx]; 14 | 15 | int n,a,b; 16 | 17 | int max_index (int u,int dist[]) 18 | { 19 | memset(vis,false,sizeof(vis)); 20 | memset(dist,0,sizeof(dist)); 21 | vis[u]=true; 22 | queueq; 23 | q.push(u); 24 | dist[u]=0; 25 | while(!q.empty()) 26 | { 27 | int u=q.front(); 28 | q.pop(); 29 | for(int i=0;i>n; 60 | for(int i=1;i<=n-1;i++) 61 | { 62 | cin>>a>>b; 63 | v[a].push_back(b); 64 | v[b].push_back(a); 65 | } 66 | int mx_dstance_indx=max_index(1,dist1); 67 | //cout< 2 | 3 | using namespace std; 4 | 5 | const int mx=10001; 6 | 7 | typedef pair pi; 8 | const int inf=1e9; 9 | vectoradj[mx]; 10 | vectorrev_adj[mx]; 11 | int dist[mx]; 12 | int dist2[mx]; 13 | bool vis[mx]; 14 | 15 | void djkstra(int src) 16 | { 17 | priority_queue,greater > pq; 18 | pq.push(make_pair(0,src)); 19 | dist[src]=0; 20 | memset(vis,false,sizeof(vis)); 21 | while(!pq.empty()) 22 | { 23 | int u=pq.top().second; 24 | pq.pop(); 25 | if(vis[u]==false) vis[u]=true; 26 | else continue; 27 | vector::iterator it; 28 | for(it=adj[u].begin();it!=adj[u].end();it++) 29 | { 30 | int v=(*it).first; 31 | int weight=(*it).second; 32 | if(dist[v]>dist[u]+weight) 33 | { 34 | dist[v]=dist[u]+weight; 35 | pq.push(make_pair(dist[v],v)); 36 | } 37 | } 38 | } 39 | } 40 | void djkstra2(int src) 41 | { 42 | priority_queue,greater > pq; 43 | pq.push(make_pair(0,src)); 44 | memset(vis,false,sizeof(vis)); 45 | dist2[src]=0; 46 | while(!pq.empty()) 47 | { 48 | int u=pq.top().second; 49 | pq.pop(); 50 | if(vis[u]==false) vis[u]=true; 51 | else continue; 52 | vector::iterator it; 53 | for(it=rev_adj[u].begin();it!=rev_adj[u].end();it++) 54 | { 55 | int v=(*it).first; 56 | int weight=(*it).second; 57 | if(dist2[v]>dist2[u]+weight) 58 | { 59 | dist2[v]=dist2[u]+weight; 60 | pq.push(make_pair(dist2[v],v)); 61 | } 62 | } 63 | } 64 | } 65 | 66 | int main() 67 | 68 | { 69 | 70 | int tc,n,m,k,s,t,a,b,c; 71 | cin>>tc; 72 | while(tc--) 73 | { 74 | cin>>n>>m>>k>>s>>t; 75 | for(int i=1;i<=n;i++) 76 | { 77 | adj[i].clear(); 78 | rev_adj[i].clear(); 79 | dist[i]=inf;dist2[i]=inf; 80 | } 81 | while(m--) 82 | { 83 | cin>>a>>b>>c; 84 | adj[a].push_back(make_pair(b,c)); 85 | rev_adj[b].push_back(make_pair(a,c)); 86 | } 87 | djkstra(s); 88 | djkstra2(t); 89 | int ans=dist[t]; 90 | //cout<>a>>b>>c; 94 | //cout< 2 | 3 | using namespace std; 4 | 5 | 6 | const int mx=505; 7 | 8 | typedef pair pi; 9 | 10 | vectoradj[mx]; 11 | 12 | bool vis[mx]; 13 | int dist[mx]; 14 | 15 | int n,mxi; 16 | 17 | void djkstra(int source) 18 | { 19 | for(int j=0;j,greater >pq; 22 | pq.push(make_pair(0,source)); 23 | dist[source]=0; 24 | while(!pq.empty()) 25 | { 26 | pi p=pq.top(); 27 | pq.pop(); 28 | int u=p.second; 29 | if(vis[u]) continue; 30 | 31 | else vis[u]=true; 32 | 33 | for(int i=0;idist[u]+weight) 38 | { 39 | dist[v]=dist[u]+weight; 40 | pq.push(make_pair(dist[v],v)); 41 | } 42 | } 43 | } 44 | } 45 | 46 | int main() 47 | 48 | { 49 | ios::sync_with_stdio(false); 50 | cin.tie(NULL); 51 | cout.tie(NULL); 52 | cin>>n; 53 | int graph[mx][mx]; 54 | for(int i=0;i>a>>b>>w; 69 | mxi=max(a,mxi); 70 | mxi=max(b,mxi); 71 | graph[a][b]=min(graph[a][b],w); 72 | graph[b][a]=min(w,graph[b][a]); 73 | } 74 | for(int i=0;i>source; 86 | djkstra(source); 87 | cin>>q; 88 | while(q--) 89 | { 90 | cin>>dest; 91 | if(vis[dest]==false) 92 | { 93 | cout<<"NO PATH"<<'\n'; 94 | } 95 | else{ 96 | cout< 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | int arr[5010]; 8 | 9 | int segment_tree(int l,int r) 10 | { 11 | if(l>r) return 0; 12 | 13 | else if(l==r&&arr[l]!=0) return 1; 14 | 15 | else if(l==r&&arr[l]==0) return 0; 16 | 17 | int ans=(r-l)+1; 18 | 19 | int mn_indx; 20 | 21 | int mx=INT_MAX; 22 | 23 | for(int i=l;i<=r;i++) if(mx>arr[i]) {mx=arr[i];mn_indx=i;} 24 | int mn=arr[mn_indx]; 25 | 26 | for(int i=l;i<=r;i++) arr[i]=arr[i]-mn; 27 | 28 | int l1 = mn_indx-1; 29 | int r1 = mn_indx+1; 30 | 31 | ans=min(ans,segment_tree(l,l1)+segment_tree(r1,r)+mn); 32 | 33 | // cout<>n; 43 | for(int i=1;i<=n;i++) 44 | { 45 | cin>>arr[i]; 46 | } 47 | ll res=segment_tree(1,n); 48 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | const int mx=101; 7 | vectoradj[mx]; 8 | bool vis[mx]; 9 | int conn=0; 10 | /* 11 | void dfs(int u) 12 | { 13 | vis[u]=true; 14 | conn++; 15 | vector::iterator it; 16 | for(it=adj[u].begin();it!=adj[u].end();it++) 17 | { 18 | if(!vis[(*it)]) 19 | { 20 | dfs((*it)); 21 | } 22 | } 23 | } 24 | */ 25 | 26 | int main() 27 | 28 | { 29 | ios::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | cout.tie(NULL); 32 | int n,s,a; 33 | cin>>n; 34 | //memset(vis,false,sizeof(vis)); 35 | for(int i=1;i<=n;i++) 36 | { 37 | for(int j=1;j<=n;j++) 38 | { 39 | cin>>a; 40 | if(a==1){ 41 | conn++; 42 | } 43 | } 44 | } 45 | //dfs(s); 46 | cout<<(conn/2)< 2 | 3 | using namespace std; 4 | 5 | 6 | const int mx=101; 7 | vectorgraph[mx]; 8 | bool vis[mx]; 9 | 10 | void dfs(int u) 11 | { 12 | vis[u]=true; 13 | vector::iterator it; 14 | for(it=graph[u].begin();it!=graph[u].end();it++) 15 | { 16 | if(!vis[(*it)]) 17 | { 18 | dfs((*it)); 19 | } 20 | } 21 | } 22 | 23 | 24 | int main() 25 | 26 | { 27 | ios::sync_with_stdio(false); 28 | cin.tie(NULL); 29 | cout.tie(NULL); 30 | int n,m,u,v; 31 | cin>>n>>m; 32 | memset(vis,false,sizeof(vis)); 33 | while(m--) 34 | { 35 | cin>>u>>v; 36 | graph[u].push_back(v); 37 | graph[v].push_back(u); 38 | } 39 | dfs(1); 40 | bool disco=false; 41 | for(int i=1;i<=n;i++) 42 | { 43 | if(!vis[i]) disco=true; 44 | } 45 | if(disco) cout<<"NO"< 2 | 3 | using namespace std; 4 | 5 | 6 | const int mx=101; 7 | vectoradj[mx]; 8 | bool vis[mx]; 9 | int conn=0; 10 | 11 | void dfs(int u) 12 | { 13 | vis[u]=true; 14 | conn++; 15 | vector::iterator it; 16 | for(it=adj[u].begin();it!=adj[u].end();it++) 17 | { 18 | if(!vis[(*it)]) 19 | { 20 | dfs((*it)); 21 | } 22 | } 23 | } 24 | 25 | 26 | int main() 27 | 28 | { 29 | ios::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | cout.tie(NULL); 32 | int n,s,a; 33 | cin>>n>>s; 34 | memset(vis,false,sizeof(vis)); 35 | for(int i=1;i<=n;i++) 36 | { 37 | for(int j=1;j<=n;j++) 38 | { 39 | cin>>a; 40 | if(a==1){ 41 | adj[i].push_back(j); 42 | adj[j].push_back(i); 43 | } 44 | } 45 | } 46 | dfs(s); 47 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | const int mx=101; 7 | vectoradj[mx]; 8 | bool vis[mx]; 9 | bool cycle=false; 10 | vector >edge; 11 | set >remove_edge; 12 | 13 | void dfs(int u,int parent) 14 | { 15 | vis[u]=true; 16 | vector::iterator it; 17 | for(it=adj[u].begin();it!=adj[u].end();it++) 18 | { 19 | if(!vis[(*it)]) 20 | { 21 | dfs((*it),u); 22 | } 23 | else if((*it)!=parent) 24 | { 25 | remove_edge.insert(make_pair((*it),u)); 26 | remove_edge.insert(make_pair(u,(*it))); 27 | } 28 | } 29 | } 30 | 31 | 32 | int main() 33 | 34 | { 35 | ios::sync_with_stdio(false); 36 | cin.tie(NULL); 37 | cout.tie(NULL); 38 | int n,m,u,v; 39 | cin>>n>>m; 40 | memset(vis,false,sizeof(vis)); 41 | while(m--) 42 | { 43 | cin>>u>>v; 44 | adj[u].push_back(v); 45 | adj[v].push_back(u); 46 | edge.push_back(make_pair(u,v)); 47 | } 48 | dfs(1,-1); 49 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | 6 | const int mx=51; 7 | vectoradj[mx]; 8 | bool vis[mx]; 9 | bool recStack[mx]; 10 | bool cycle=false; 11 | 12 | bool cy_dfs(int u,int parent) 13 | { 14 | vis[u]=true; 15 | recStack[u]=true; 16 | vector::iterator it; 17 | for(it=adj[u].begin();it!=adj[u].end();it++) 18 | { 19 | if(!vis[(*it)]) 20 | { 21 | if(cy_dfs((*it),u)){ 22 | return true;} 23 | } 24 | else if(recStack[(*it)]) 25 | { 26 | return true; 27 | } 28 | } 29 | recStack[u]=false; 30 | return false; 31 | } 32 | 33 | 34 | int main() 35 | 36 | { 37 | ios::sync_with_stdio(false); 38 | cin.tie(NULL); 39 | cout.tie(NULL); 40 | int n,m,a; 41 | cin>>n; 42 | memset(vis,false,sizeof(vis)); 43 | memset(recStack,false,sizeof(recStack)); 44 | for(int i=1;i<=n;i++) 45 | { 46 | for(int j=1;j<=n;j++) 47 | { 48 | cin>>a; 49 | if(a==1){ 50 | adj[i].push_back(j); 51 | } 52 | } 53 | } 54 | for(int i=1;i<=n;i++) 55 | { 56 | if(!vis[i]) // This node has been not visited , checking for different connected components 57 | { 58 | if(cy_dfs(i,-1)) 59 | { 60 | cycle=true; 61 | } 62 | } 63 | } 64 | string res=(cycle==true)?"1":"0"; 65 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | const int mx=101; 7 | vectoradj[mx]; 8 | bool vis[mx]; 9 | int conn=0; 10 | /* 11 | void dfs(int u) 12 | { 13 | vis[u]=true; 14 | conn++; 15 | vector::iterator it; 16 | for(it=adj[u].begin();it!=adj[u].end();it++) 17 | { 18 | if(!vis[(*it)]) 19 | { 20 | dfs((*it)); 21 | } 22 | } 23 | } 24 | */ 25 | 26 | int main() 27 | 28 | { 29 | ios::sync_with_stdio(false); 30 | cin.tie(NULL); 31 | cout.tie(NULL); 32 | int n,m,u,v; 33 | cin>>n>>m; 34 | //memset(vis,false,sizeof(vis)); 35 | while(m--) 36 | { 37 | cin>>u>>v; 38 | adj[u].push_back(v); 39 | adj[v].push_back(u); 40 | } 41 | for(int i=1;i<=n;i++) 42 | { 43 | cout<