1 / 7
May 2023

#include <bits/stdc++.h>
using namespace std;
const int INF=1e9+7;
const int N=1e6+10;

int n,r,m;
vector<pair<int,int>> s;

vector<vector> g(N);
vector level(N);

void reset(){
for(int i=0;i<N;i++) level[i]=INF;
}

void bfs(){

queue<pair<int,int>> q;
for(int i=0;i<s.size();i++){
    q.push({s[i].first,s[i].second});
    level[s[i].first]=0;
}

while(!q.empty()){

    auto v=q.front();
    q.pop();

    int i=v.first,j=v.second;

    for(auto &childi:g[i]){

        if(j>0){
            if((level[i]+1)<level[childi]){
                level[childi]=level[i]+1;
                q.push({childi,j-1});
            }
        }
    }
}

for(int i=1;i<=n;i++){
    
    if(level[i]==INF){
        
        cout<<"No"<<endl;
        return;
    }
    
}

cout<<"Yes"<<endl;

}

int main(){
int t;
cin>>t;
while(t–){
cin>>n>>r>>m;
reset();

    for(int i=0;i<r;i++){
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    for(int i=0;i<m;i++){
        int k,S;
        cin>>k>>S;
        s.push_back({k,S});
    }

    bfs();
}
return 0;

}

  • created

    May '23
  • last reply

    Jun '23
  • 6

    replies

  • 590

    views

  • 5

    users

Two problems:

  1. You don’t clear the graph between test cases.

  2. You haven’t checked whether any city is guarded by more than one guard, e.g. the answer to this case should be NO.

1
2 1 2
1 2
1 2
2 2

Yeah , got an AC. I was considering that the soldier had choice whether to protect a reachable city or not. Thats where i went wrong. Above testcase made me realize that , didnt read the question properly,silly mistakes :frowning:
Thanks buddy.

int n,r,m;
vector<int> adj[W];
bool visited[N];
int dist[N];
vector<pair<int,int>>v(N);
void reset(){

for(int i=0;i<N;i++) dist[i]=INF;
}

void bfs(){

  queue<pii>q;
for(int i=0;i<v.size();i++){
	q.push({v[i].first,v[i].second});
	visited[v[i].first]=true;
	dist[v[i].first]=0;
	
}

while(!q.empty()){
	
	int c=q.front().first;
	int d=q.front().second;
	q.pop();
	//cout<<c<<endl;
	for(int u:adj[c]){
		//cout<<u<<endl;
		dist[u]=dist[c]+1;
		if(dist[u]<=d){
			if(visited[u]){
				cout<<"NO"<<endl;
				return;
			}
			else{
				visited[u]=true;
				q.push({u,d-dist[u]});
			}
		}
	}
}







for(int i=1;i<=n;i++){
	if(dist[i]==INF){
		cout<<"NO"<<endl;
		return;
	}
}

cout<<"YES"<<endl;
return;

}

void solve(){

cin>>n>>r>>m;
reset();


rep(i,0,r){
	int x,y;
	cin>>x>>y;
	adj[x].pb(y);
	adj[y].pb(x);
}



rep(i,0,m){
	int x,y;
	cin>>x>>y;
	v.pb({x,y});
}

bfs();

}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// freopen(“input.txt”, “r”, stdin);
// freopen(“output.txt”, “w”, stdout);
#ifdef SEIVE
seive();
#endif
#ifdef NCR
init();
#endif
#ifdef DSU
cleardsu(MAXDSUSIZE);
#endif

int t=1;
cin>>t;
while(t--)solve();
return 0;

}

bro here is my code its running fine for single input but it not working fine for set of inputs

Where do you clear “adj” and “visited” for test cases after the first?

where am i doing wrong am i missing something

#include
#include <bits/stdc++.h>
// #include <sys/resource.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace chrono;
// using namespace __gnu_pbds;

// def
// #define Sahib 1

// Speed
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Asquare cout.tie(NULL);

// Aliases
using ll = long long;
using lld = long double;
using ull = unsigned long long;

// Constants
const lld pi = 3.141592653589793238;
const ll INF = LONG_LONG_MAX;
const ll mod = 1e9 + 7;

// TypeDEf
typedef pair<ll, ll> pll;
typedef vector vll;
typedef vector vpll;
typedef vector vs;
typedef unordered_map<ll, ll> umll;
typedef map<ll, ll> mll;

// Macros
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define fl(i, n) for (int i = 0; i < n; i++)
#define rl(i, m, n) for (int i = n; i >= m; i–)
#define py cout << “YES\n”;
#define pm cout << “-1\n”;
#define pn cout << “NO\n”;
#define vr(v) v.begin(), v.end()
#define rv(v) v.end(), v.begin()

// Debug
#ifdef Sahib
#define debug(x)
cerr << #x << " ";
cerr << x << " ";
cerr << endl;
#else
#define debug(x) ;
#endif

// Operator overloads
template <typename T1, typename T2> // cin >> pair<T1, T2>
istream &operator>>(istream &istream, pair<T1, T2> &p)
{
return (istream >> p.first >> p.second);
}
template // cin >> vector
istream &operator>>(istream &istream, vector &v)
{
for (auto &it : v)
cin >> it;
return istream;
}
template <typename T1, typename T2> // cout << pair<T1, T2>
ostream &operator<<(ostream &ostream, const pair<T1, T2> &p)
{
return (ostream << p.first << " " << p.second);
}
template // cout << vector
ostream &operator<<(ostream &ostream, const vector &c)
{
for (auto &it : c)
cout << it << " ";
return ostream;
}

// Utility functions
template
void print(T &&t) { cout << t << “\n”; }
void printarr(ll arr[], ll n)
{
fl(i, n) cout << arr[i] << " ";
cout << “\n”;
}
template
void printvec(vector v)
{
ll n = v.size();
fl(i, n) cout << v[i] << " ";
cout << “\n”;
}
template
ll sumvec(vector v)
{
ll n = v.size();
ll s = 0;
fl(i, n) s += v[i];
return s;
}

// Mathematical functions
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
} //__gcd
ll lcm(ll a, ll b) { return (a / gcd(a, b) * b); }

ll moduloMultiplication(ll a, ll b, ll mod)
{
ll res = 0;
a %= mod;
while (b)
{
if (b & 1)
res = (res + a) % mod;
b >>= 1;
}
return res;
}
ll powermod(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}

// Graph-dfs
// bool gone[MN];
// vector adj[MN];
// void dfs(int loc){
// gone[loc]=true;
// for(auto x:adj[loc])if(!gone[x])dfs(x);
// }

// Sorting
bool sorta(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); }
bool sortd(const pair<int, int> &a, const pair<int, int> &b) { return (a.second > b.second); }

// Bits
string decToBinary(int n)
{
string s = “”;
int i = 0;
while (n > 0)
{
s = to_string(n % 2) + s;
n = n / 2;
i++;
}
return s;
}
ll binaryToDecimal(string n)
{
string num = n;
ll dec_value = 0;
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i–)
{
if (num[i] == ‘1’)
dec_value += base;
base = base * 2;
}
return dec_value;
}

// Check
bool isPrime(ll n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
bool isPowerOfTwo(int n)
{
if (n == 0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
bool isPerfectSquare(ll x)
{
if (x >= 0)
{
ll sr = sqrt(x);
return (sr * sr == x);
}
return false;
}

// Code
void asquare()
{
ll n;
cin >> n;
}
// Main
int main()
{

int T;
cin >>T;

for (int t = 0; t < T; t++) {
    int N, R, M;
    cin >> N >> R >> M;

    vector<vector<int>> roads(N+1);
    vector<pair<int,int>> strength;

    for (int r = 0; r < R; r++) {
        int a, b;
        cin >> a >> b;
       // a--; b--; // Decrement by 1 to use 0-indexed city numbers
        roads[a].push_back(b);
        roads[b].push_back(a);
    }

    for (int m = 0; m < M; m++) {
		int K , S;
		cin>>K>>S;
		strength.push_back({K,S});           
    }
    
    
    unordered_map<int,bool> done;
    bool flag = true;
    for(auto x:strength)
    {
		unordered_map<int,bool> visited;
		unordered_map<int,int> distance;
		int city = x.first;
		visited[city] = false;
		distance[city] = 0;
		int strong = x.second;
		if(strong<distance[city])
		{
			break;
		}
		queue<int> q;
		q.push(city);  
		visited[city]=true;
		distance[city]=0;
		while(!q.empty())
		{
			int curr = q.front();
			q.pop();
			if(done[curr])
			{
				//cout<<"already "<<curr<<" is done "<<" city " <<city <<endl;
				flag = false;
				break;
			}
			
			for(auto nbr:roads[curr])
			{
				if(!visited[nbr])
				{
					distance[nbr] = distance[curr]+1;
					if(distance[nbr]<=strong)
					{
						visited[nbr] = true;
						q.push(nbr);
					}
					visited[nbr] = true;
					
				}
				
			}
			done[curr] = true;
			//cout<<"done : "<<curr<<endl;
				
			
		}
		if(!flag)
		{
			break;
		}
		
	}
	
	if(!flag)
	{
		cout<<"NO"<<endl;
	}
	else
	{
		cout<<"Yes"<<endl;
	}

    
}


return 0;

}
// End