Lightoj 1263 - Equalizing Money
#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 = 1002;
int money[high] , visited[high] , person=0;
LL sum = 0;
vii adj[high];
void DFS(int u)
{
visited[u] = 1;
sum += money[u];
person += 1;
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i];
if(!visited[v])
{
DFS(v);
}
}
}
void CLR()
{
for(int i=0; i<=high; i++) adj[i].clear();
}
int main()
{
int test , tc=0 , N , M , i , u , v;
LL Rsum=0;
sf("%d", &test);
while(test--)
{
CLR();
mset(visited , 0);
//mset(exist , 0);
sum = 0;
person = 0;
Rsum = 0;
sf("%d %d", &N , &M);
for(i=1; i<=N; i++)
{
sf("%d", &money[i]);
Rsum += money[i];
}
//cout << "money by node:\n";
//for(i=1; i<=N; i++) cout << money[i] << "; ";
int avg = Rsum / N; // have to check each person have same amount of money
for(i=1; i<=M; i++)
{
sf("%d %d" , &u , &v);
adj[u].psb(v);
adj[v].psb(u);
//exist[u] = 1;
//exist[v] = 1;
}
bool distribution = true;
pf("Case %d: " , ++tc);
for(i=1; i<=N; i++)
{
if(!visited[i])
{
sum = 0;
person = 0;
DFS(i);
//cout << "node " << i << ":\n" << "person = " << person << " sum =" << sum << "\n";
if(sum % person != 0)
{
distribution = false;
break;
}
else
{
if(avg != (sum / person)) // every person even a non-relation person have same amount money(knock at avg)
{
distribution = false;
break;
}
}
}
}
//outn(sum);
pf(distribution ? "Yes\n" : "No\n");
}
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 = 1002;
int money[high] , visited[high] , person=0;
LL sum = 0;
vii adj[high];
void DFS(int u)
{
visited[u] = 1;
sum += money[u];
person += 1;
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i];
if(!visited[v])
{
DFS(v);
}
}
}
void CLR()
{
for(int i=0; i<=high; i++) adj[i].clear();
}
int main()
{
int test , tc=0 , N , M , i , u , v;
LL Rsum=0;
sf("%d", &test);
while(test--)
{
CLR();
mset(visited , 0);
//mset(exist , 0);
sum = 0;
person = 0;
Rsum = 0;
sf("%d %d", &N , &M);
for(i=1; i<=N; i++)
{
sf("%d", &money[i]);
Rsum += money[i];
}
//cout << "money by node:\n";
//for(i=1; i<=N; i++) cout << money[i] << "; ";
int avg = Rsum / N; // have to check each person have same amount of money
for(i=1; i<=M; i++)
{
sf("%d %d" , &u , &v);
adj[u].psb(v);
adj[v].psb(u);
//exist[u] = 1;
//exist[v] = 1;
}
bool distribution = true;
pf("Case %d: " , ++tc);
for(i=1; i<=N; i++)
{
if(!visited[i])
{
sum = 0;
person = 0;
DFS(i);
//cout << "node " << i << ":\n" << "person = " << person << " sum =" << sum << "\n";
if(sum % person != 0)
{
distribution = false;
break;
}
else
{
if(avg != (sum / person)) // every person even a non-relation person have same amount money(knock at avg)
{
distribution = false;
break;
}
}
}
}
//outn(sum);
pf(distribution ? "Yes\n" : "No\n");
}
return 0;
}
Comments
Post a Comment