You are given cell position (row and column) of demons in 2D grid. In a single attack you can kill all demons in a row or in a column. You have to print minimum number of attack needed to kill all the demons.
Observation: number of demons <=1000 and given cell position (row number or column number) can be 10^9. so you have to decompress the row numbers and column numbers using STL map.
Solution Idea #1: Brute Force.
count how many demons in row and column. find which row or column have maximum demons, then remove all the demons belongs to it.
the idea won't work for this configuration.
brute force idea will give 4, where minimum is 3. now go to the next idea.
Solution Idea #2: Bipartite Matching
think the problem a little differently. each demon is denoted by two value, row and column. now you can assume the demons as edge and row and column as nodes. look at the picture below to clarify.
here is two edge denoted by nodes (1, 7) and (2, 5)
now your task is to find minimum number of nodes to cover all edges.
BINGO!!! straightforward BPM.
Code:
- #include<bits/stdc++.h>
- using namespace std;
- #define SET(a) memset(a,-1,sizeof(a))
- #define CLR(a) memset(a,0,sizeof(a))
- #define PI acos(-1.0)
- #define MOD 1000000007
- #define MX 2002
- vector<int>adj[MX];
- int lft[MX], rgt[MX];
- bool visited[MX];
- bool bpm(int u)
- {
- for(int i=0;i<adj[u].size();i++)
- {
- int v=adj[u][i];
- if(visited[v]) continue;
- visited[v]=true;
- if(rgt[v]==-1 || bpm(rgt[v]))
- {
- lft[u]=v;
- rgt[v]=u;
- return true;
- }
- }
- return false;
- }
- int maximum_match(int n)
- {
- int cnt=0;
- for(int i=1;i<=n;i++)
- {
- CLR(visited);
- if(bpm(i)) cnt++;
- }
- return cnt;
- }
- int main()
- {
- int tc,kk=1, n, x, y;
- cin>>tc;
- while(tc--)
- {
- cin>>n;
- map<int, int>mp;
- int cnt=0;
- for(int i=0;i<n;i++)
- {
- cin>>x>>y;
- y+=1e9+1;
- if(!mp[x]) mp[x]=++cnt;
- if(!mp[y]) mp[y]=++cnt;
- adj[mp[x]].push_back(mp[y]);
- adj[mp[y]].push_back(mp[x]);
- }
- SET(lft);
- SET(rgt);
- cout<<maximum_match(cnt)/2<<endl;
- for(int i=1;i<=cnt;i++) adj[i].clear();
- }
- return 0;
- }
Nice Explanation !!
ReplyDeletethank you for your feedback. :)
ReplyDeleteI've had this same experience . I use to see 4 demons all the time, I feel this numbing feeling over my body then i am held down Ive been choked before. Sometimes i see them when i open my eyes after the attack. I think they come while you'r sleeping because your mind is open.This has happened all my life and i always wondered why. I wasnt religious and trying to fight them on my own, at this time i was confuse and almost running mad. not until a friend of my Johnson pen told me about a prophet who helped him in the same problem too. i email the prophet and i told him my problem and i did what he asked of me,he teach me the understanding of this demons and how to fight them with your holy bible,
ReplyDeletewanna give prophet michael a big appreciation for bringing back peace and joy to me, if not for you i woundn't have be able to give my testimony today....
I have promise to link all human that's undergoing this situation to prophet michael, because i know how it is hurt....
prophetmichaelfk@gmail.com
that's the e-mail of the prophet, give him a try and share your testimony with alot of joy...
Good Luck pals........
God bless you
How are you drawing image in each problem ? Which tool use ?
ReplyDeletesorry typing error. Which tool do you use to draw diagrams ?
DeleteThe simplest one, paint tool. :P
Deletei run into your blog, its great. :)
DeleteThanks sir :)
Delete