UVa 567 - Risk
/*
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
// BFS
#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 mset(c,v) memset(c , v , sizeof c)
#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 = 25;
vii adj[high];
int cost[high], visited[high];
int BFS(int s , int ter)
{
visited[s] = 1;
cost[s] = 0;
int u , v=0;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
u = Q.front();
Q.pop();
vii::iterator it;
for(it=adj[u].begin(); it!=adj[u].end(); it++)
{
v = *it;
if(v == ter) return cost[u] + 1;
if(!visited[v])
{
visited[v] = 1;
cost[v] = cost[u] + 1;
Q.push(v);
}
}
}
}
void CLR()
{
for(int i=0; i<high; i++)
{
adj[i].clear();
}
}
int main()
{
fast;
int x , i , T , N , y , tc=0;
while(~sf("%d", &T))
{
CLR();
while(T--)
{
sf("%d", &x);
adj[1].psb(x);
adj[x].psb(1);
}
for(i=2; i<=19; i++)
{
sf("%d", &T);
while(T--)
{
sf("%d", &x);
adj[i].psb(x);
adj[x].psb(i);
}
}
//cin >> N;
sf("%d", &N);
//cout << "Test Set #" << ++tc << "\n";
pf("Test Set #%d\n", ++tc);
while(N--)
{
mset(visited , 0);
mset(cost , 0);
//cin >> x >> y;
sf("%d %d", &x , &y);
int moves = BFS(x , y);
//cout << moves << "\n";
pf("%2d to %2d: %d\n", x , y , moves);
}
pf("\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
// BFS
#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 mset(c,v) memset(c , v , sizeof c)
#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 = 25;
vii adj[high];
int cost[high], visited[high];
int BFS(int s , int ter)
{
visited[s] = 1;
cost[s] = 0;
int u , v=0;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
u = Q.front();
Q.pop();
vii::iterator it;
for(it=adj[u].begin(); it!=adj[u].end(); it++)
{
v = *it;
if(v == ter) return cost[u] + 1;
if(!visited[v])
{
visited[v] = 1;
cost[v] = cost[u] + 1;
Q.push(v);
}
}
}
}
void CLR()
{
for(int i=0; i<high; i++)
{
adj[i].clear();
}
}
int main()
{
fast;
int x , i , T , N , y , tc=0;
while(~sf("%d", &T))
{
CLR();
while(T--)
{
sf("%d", &x);
adj[1].psb(x);
adj[x].psb(1);
}
for(i=2; i<=19; i++)
{
sf("%d", &T);
while(T--)
{
sf("%d", &x);
adj[i].psb(x);
adj[x].psb(i);
}
}
//cin >> N;
sf("%d", &N);
//cout << "Test Set #" << ++tc << "\n";
pf("Test Set #%d\n", ++tc);
while(N--)
{
mset(visited , 0);
mset(cost , 0);
//cin >> x >> y;
sf("%d %d", &x , &y);
int moves = BFS(x , y);
//cout << moves << "\n";
pf("%2d to %2d: %d\n", x , y , moves);
}
pf("\n");
}
return 0;
}
Comments
Post a Comment