Could anyone please help me find out what's wrong in my code. I'm getting wrong answer. Probably i didn't understand the question properly.
This is the link to the question. https://www.spoj.com/problems/AKBAR/
Below is my code.
include<bits/stdc++.h>
using namespace std; void dfs(vector<vector> &city,vector &visited,int s,int strength,map<int,int> &soldier) { visited[s] = true; for(int i = 0; i < city[s].size(); ++i) { if(visited[city[s][i]] == false && strength > 0 && soldier.find(city[s][i]) == soldier.end()) dfs(city,visited,city[s][i],strength-1,soldier); } }
int main() { int t; scanf("%d",&t); while(t--) { int n,r,m; scanf("%d%d%d",&n,&r,&m); vector<vector> city(n+1); vector visited(n+1); while(r--) { int a,b; scanf("%d%d",&a,&b); city[a].push_back(b); city[b].push_back(a); } map<int,int> soldier; while(m--) { int k,s; scanf("%d%d",&k,&s); soldier.insert({k,s}); } for(auto it = soldier.begin(); it != soldier.end(); ++it) dfs(city,visited,it->first,it->second,soldier); int i; for(i = 1; i <= n; ++i) if(visited[i] == false) { printf("No\n"); break; } if(i > n) printf("Yes\n"); } return 0; }
Thanks.
Your code isn't fit for the bold part of the question. This program may output "Yes" when a city is protected by more than one soldiers.
I modified my code a little bit according to your comment. Please see if i did anything wrong. I'm still getting wrong answer.
include<bits/stdc++.h>
using namespace std; void dfs(vector<vector> &city,vector &visited,int s,int strength,map<int,int> &soldier) { visited[s] = true; for(int i = 0; i < city[s].size(); ++i) { if(visited[city[s][i]] == false && strength > 0 && soldier.find(city[s][i]) == soldier.end()) dfs(city,visited,city[s][i],strength-1,soldier); } }
int main() { int t; scanf("%d",&t); while(t--) { int n,r,m; scanf("%d%d%d",&n,&r,&m); vector<vector> city(n+1); vector visited(n+1); while(r--) { int a,b; scanf("%d%d",&a,&b); city[a].push_back(b); city[b].push_back(a); } map<int,int> soldier; bool temp = false; while(m--) { int k,s; scanf("%d%d",&k,&s); if(soldier.find(k) != soldier.end()) temp = true; else soldier.insert({k,s}); } if(temp == true) printf("No\n"); else { for(auto it = soldier.begin(); it != soldier.end(); ++it) dfs(city,visited,it->first,it->second,soldier); int i; for(i = 1; i <= n; ++i) if(visited[i] == false) { printf("No\n"); break; } if(i > n) printf("Yes\n"); } } return 0; }
Thanks.
You must check the duplicates of soldiers for all dfs visits(since soldiers protect them). Frankly speaking, It's difficult for me to tell the way to fix with words. You may make dfs function like this:
I mean, try filling
visited
with positive ids of soldiers(such asm+1
). If a city is protected by two soldiers, two ids will overridevisited[s]
, executesduplicate = true
. If it's protected by no ones,visited
will have 0.Let me write my best advice for you.
First, I'll explain with words. In the previous advice, I used "ids" instead of bool. This is to identify another soldier's visits. In this problem, dfs may make duplicated visits of one soldier(and this should be ignored).
Second, try reading this full code.
Notice: I didn't submit this, so this program may be not accepted.
Finally, if you don't understand what I said, (maybe it comes from my dirty English, or my answer is wrong!) I recommend you to leave this problem unsolved and to come back later. Also recommend drawing graph if you didn't.