UVa 627 - The Net
/*
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 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 = 500;
char ch[high] , *token;
vii adj[high];
bool visited[high];
int par[high] , start , finish;
bool BFS(int s)
{
visited[s] = true;
queue<int>Q;
Q.push(s);
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])
{
visited[v] = true;
par[v] = u;
if(v == finish) return true;
Q.push(v);
}
}
}
return false;
}
void Print_Path(int u)
{
if(start == u) pf("%d", start);
else
{
Print_Path(par[u]);
pf(" %d", u);
}
}
void CLR(int n)
{
for(int i=0; i<n+1; i++)
{
adj[i].clear();
}
}
int main()
{
//fast;
int n , x , y;
while(~sf("%d", &n))
{
getchar();
CLR(n);
while(n--)
{
gets(ch);
token = strtok(ch , "-,");
x = atoi(token);
//pf("%d\n" , x);
token = strtok(NULL, "-,");
while(token != NULL)
{
y = atoi(token);
//pf("%d; ", y);
adj[x].psb(y);
token = strtok(NULL , "-,");
}
}
int m;
sf("%d", &m);
printf("-----\n");
while(m--)
{
mset(par , 0);
mset(visited , false);
sf("%d %d", &start , &finish);
bool flg = BFS(start);
if(flg)
{
Print_Path(finish);
pf("\n");
}
else pf("connection impossible\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 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 = 500;
char ch[high] , *token;
vii adj[high];
bool visited[high];
int par[high] , start , finish;
bool BFS(int s)
{
visited[s] = true;
queue<int>Q;
Q.push(s);
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])
{
visited[v] = true;
par[v] = u;
if(v == finish) return true;
Q.push(v);
}
}
}
return false;
}
void Print_Path(int u)
{
if(start == u) pf("%d", start);
else
{
Print_Path(par[u]);
pf(" %d", u);
}
}
void CLR(int n)
{
for(int i=0; i<n+1; i++)
{
adj[i].clear();
}
}
int main()
{
//fast;
int n , x , y;
while(~sf("%d", &n))
{
getchar();
CLR(n);
while(n--)
{
gets(ch);
token = strtok(ch , "-,");
x = atoi(token);
//pf("%d\n" , x);
token = strtok(NULL, "-,");
while(token != NULL)
{
y = atoi(token);
//pf("%d; ", y);
adj[x].psb(y);
token = strtok(NULL , "-,");
}
}
int m;
sf("%d", &m);
printf("-----\n");
while(m--)
{
mset(par , 0);
mset(visited , false);
sf("%d %d", &start , &finish);
bool flg = BFS(start);
if(flg)
{
Print_Path(finish);
pf("\n");
}
else pf("connection impossible\n");
}
}
return 0;
}
Comments
Post a Comment