UVa 924 - Spreading The News
Thanks to http://acmph.blogspot.com/2010/10/924-spreading-news-uva.html
/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.
*/
// Accepted
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 3000;
struct printf
{
void ek(int n){cout << n << "\n";}
void dui(int x , int y) { cout << x << " " << y << "\n"; }
void tin(int x , int y , int z) { cout << x << " " << y << " " << z << "\n"; }
}tp;
struct sieve
{
int prm[high], plen=0;
bitset<high>bs;
void get_prime()
{
LL i , j;
bs.set();
bs[0]=bs[1]=0;
for(i=2; i<=high; i++)
{
if(bs[i])
{
prm[plen++] = i;
for(j=i*i; j<=high; j+=i) bs[j] = 0;
}
}
}
void pprm(){ for(int i=0; i<100; i++) cout << prm[i] << "; "; }
}prime;
vii adj[high];
int boom[high] , level[high]; // boom means maximum number of employees
bool visited[high];
void adj_clear()
{
for(int i=0; i<high; i++) adj[i].clear();
}
void BFS(int s)
{
queue<int>Q;
Q.push(s);
visited[s] = true;
level[s] = 0;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i];
if(!visited[v])
{
level[v] = level[u] + 1;
boom[ level[v] ]++; // Number of Employees
visited[v] = true;
Q.push(v);
}
}
}
}
int main()
{
//freopen("in.txt" , "r" , stdin);
fast;
adj_clear();
int i , em , employee , n , x , test=0;
cin >> employee;
for(em=0; em < employee; em++)
{
cin >> n;
while(n--)
{
cin >> x;
adj[em].psb(x);
}
}
cin >> test;
while(test--)
{
memset(level , 0 , sizeof level);
memset(visited , false , sizeof visited);
memset(boom , 0 , sizeof boom);
cin >> x;
if(!adj[x].size())
{
cout << "0\n";
continue;
}
else
{
BFS(x);
int mx = -1 , day = 0;
for(i=0; i<employee; i++)
{
if(mx < boom[i])
{
mx = boom[i];
day = i;
}
}
cout << mx << " " << day << "\n";
}
}
return 0;
}
/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.
*/
// Accepted
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 3000;
struct printf
{
void ek(int n){cout << n << "\n";}
void dui(int x , int y) { cout << x << " " << y << "\n"; }
void tin(int x , int y , int z) { cout << x << " " << y << " " << z << "\n"; }
}tp;
struct sieve
{
int prm[high], plen=0;
bitset<high>bs;
void get_prime()
{
LL i , j;
bs.set();
bs[0]=bs[1]=0;
for(i=2; i<=high; i++)
{
if(bs[i])
{
prm[plen++] = i;
for(j=i*i; j<=high; j+=i) bs[j] = 0;
}
}
}
void pprm(){ for(int i=0; i<100; i++) cout << prm[i] << "; "; }
}prime;
vii adj[high];
int boom[high] , level[high]; // boom means maximum number of employees
bool visited[high];
void adj_clear()
{
for(int i=0; i<high; i++) adj[i].clear();
}
void BFS(int s)
{
queue<int>Q;
Q.push(s);
visited[s] = true;
level[s] = 0;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i];
if(!visited[v])
{
level[v] = level[u] + 1;
boom[ level[v] ]++; // Number of Employees
visited[v] = true;
Q.push(v);
}
}
}
}
int main()
{
//freopen("in.txt" , "r" , stdin);
fast;
adj_clear();
int i , em , employee , n , x , test=0;
cin >> employee;
for(em=0; em < employee; em++)
{
cin >> n;
while(n--)
{
cin >> x;
adj[em].psb(x);
}
}
cin >> test;
while(test--)
{
memset(level , 0 , sizeof level);
memset(visited , false , sizeof visited);
memset(boom , 0 , sizeof boom);
cin >> x;
if(!adj[x].size())
{
cout << "0\n";
continue;
}
else
{
BFS(x);
int mx = -1 , day = 0;
for(i=0; i<employee; i++)
{
if(mx < boom[i])
{
mx = boom[i];
day = i;
}
}
cout << mx << " " << day << "\n";
}
}
return 0;
}
Comments
Post a Comment