UVa 10959 - The Party, Part I
// Accepted
#include<bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define psb push_back
#define fast ios_base::sync_with_stdio(false)
const int high = 2010;
int visited[high] , dis[high];
vector<int>adj[high];
void CLR()
{
for(int i=0; i<high; i++)
{
visited[i] = 0;
dis[i] = 0;
adj[i].clear();
}
}
void BFS(int s)
{
visited[s] = 1;
dis[s] = 0;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
int sze = adj[u].size();
for(int i=0; i<sze; i++)
{
int v = adj[u][i];
if(!visited[v])
{
dis[v] = dis[u] + 1;
visited[v] = 1;
Q.push(v);
}
}
}
}
int main()
{
fast;
int i, test;
cin >> test;
while(test--)
{
CLR();
int P, D;
cin >> P >> D;
for(i=0; i<D; i++)
{
int u , v;
cin >> u >> v;
adj[u].psb(v);
adj[v].psb(u);
}
BFS(0);
for(i=1; i<P; i++)
{
cout << dis[i] << "\n";
}
if(test) cout << "\n";
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define psb push_back
#define fast ios_base::sync_with_stdio(false)
const int high = 2010;
int visited[high] , dis[high];
vector<int>adj[high];
void CLR()
{
for(int i=0; i<high; i++)
{
visited[i] = 0;
dis[i] = 0;
adj[i].clear();
}
}
void BFS(int s)
{
visited[s] = 1;
dis[s] = 0;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
int sze = adj[u].size();
for(int i=0; i<sze; i++)
{
int v = adj[u][i];
if(!visited[v])
{
dis[v] = dis[u] + 1;
visited[v] = 1;
Q.push(v);
}
}
}
}
int main()
{
fast;
int i, test;
cin >> test;
while(test--)
{
CLR();
int P, D;
cin >> P >> D;
for(i=0; i<D; i++)
{
int u , v;
cin >> u >> v;
adj[u].psb(v);
adj[v].psb(u);
}
BFS(0);
for(i=1; i<P; i++)
{
cout << dis[i] << "\n";
}
if(test) cout << "\n";
}
return 0;
}
Comments
Post a Comment