LightOj 1357 - Corrupted Friendship
#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 = 100002;
vii adj[high];
int visited[high];
LL nodeVisited=0 , notFriend=0;
int N;
void CLR()
{
for(int i=0; i<high; i++) adj[i].clear();
}
void DFS(int u)
{
visited[u] = 1;
nodeVisited += 1;
int sz = adj[u].size();
for(int i=0; i<sz; i++)
{
int v = adj[u][i];
if(!visited[v])
{
DFS(v);
}
}
notFriend += (N - nodeVisited);
//cout << notFriend << "; ";
}
int main()
{
int u , v , test ,tc=0;
sf("%d", &test);
while(test--)
{
CLR();
mset(visited , 0);
sf("%d", &N);
for(int i=1; i<=N-1; i++)
{
sf("%d %d", &u , &v);
adj[u].psb(v);
adj[v].psb(u);
}
nodeVisited = 0;
notFriend = 0;
int invitationMade = N - 1;
DFS(1);
pf("Case %d: %d %lld\n", ++tc , invitationMade , notFriend);
}
return 0;
}
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 = 100002;
vii adj[high];
int visited[high];
LL nodeVisited=0 , notFriend=0;
int N;
void CLR()
{
for(int i=0; i<high; i++) adj[i].clear();
}
void DFS(int u)
{
visited[u] = 1;
nodeVisited += 1;
int sz = adj[u].size();
for(int i=0; i<sz; i++)
{
int v = adj[u][i];
if(!visited[v])
{
DFS(v);
}
}
notFriend += (N - nodeVisited);
//cout << notFriend << "; ";
}
int main()
{
int u , v , test ,tc=0;
sf("%d", &test);
while(test--)
{
CLR();
mset(visited , 0);
sf("%d", &N);
for(int i=1; i<=N-1; i++)
{
sf("%d %d", &u , &v);
adj[u].psb(v);
adj[v].psb(u);
}
nodeVisited = 0;
notFriend = 0;
int invitationMade = N - 1;
DFS(1);
pf("Case %d: %d %lld\n", ++tc , invitationMade , notFriend);
}
return 0;
}
Comments
Post a Comment