Skip to content

Add ECPC 2017 solutions #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions Codeforces/CF101840-GYM-D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// AC
#include <bits/stdc++.h>
using namespace std;

int a[100000];
int vis[100000] = {};
int t;
vector<queue<int> > divisors(100001);

vector<int> parent;

int find(int node)
{
if (node == parent[node])
return node;
return parent[node] = find(parent[node]);
}

void merge(int a, int b)
{
int pa = find(a);
int pb = find(b);
if (pa==pb)return;
// merge
parent[pa] = pb;
}

int main()
{
freopen("dream.in", "r", stdin);
int T;
cin>>T;
for (t=1;t<=T;t++){
int n;
cin>>n;
parent = vector<int> (n);
for (int i=0;i<n;i++){
cin>>a[i];
parent[i] = i;
for (long long int div=1; div*div<=a[i]; div++)
{
if (a[i] % div == 0)
{
if (div*div == a[i])
{
divisors[div].push(i);
}
else
{
divisors[div].push(i);
divisors[a[i]/div].push(i);
}
}
}
}
long long int ans = 0;
if (n!=1)
{
for (int div=100000; div>=1; div--){
queue<int> & q = divisors[div];
if (q.empty())
{
continue;
}
if (q.size()==1)
{
q.pop();
continue;
}
else
{
int n_new = 0;
int gcd_n = q.size();
// add edges for finding components
int root = -1;
queue<int> new_nodes;
queue<int> old_nodes;
while(!q.empty())
{
int node = q.front();
q.pop();
if (vis[node] != t)
{
vis[node] = t;
new_nodes.push(node);
n_new++;
}
else
{
old_nodes.push(node);
root = node;
}
}
if (gcd_n == n_new){
root = new_nodes.front();
new_nodes.pop();
}
while(!new_nodes.empty()){
int node = new_nodes.front();
new_nodes.pop();
ans += div;
merge(node, root);
}

if (! old_nodes.empty())
{
int node_first = old_nodes.front();
old_nodes.pop();
while(!old_nodes.empty())
{
int node_second = old_nodes.front();
old_nodes.pop();
int pf = find(node_first);
int ps = find(node_second);
if (pf != ps)
{
//merge
merge(node_second, node_first);
ans+= div;
}
}
}
}
}
}
printf("Case %d: %lld\n", t, ans);
}
}
14 changes: 14 additions & 0 deletions Codeforces/CF101840-GYM-K.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'''
AC

Notes:
- Max no of doals is at most = no of bodies
- The second choice isn't good (First choice is always better than it)
- It's better to use just one eye and one mouth than using two eyes
'''

with open('katryoshka.in', 'r') as f:
T = int(f.readline())
for t in range(1, T+1):
n, m, k = [int(no) for no in f.readline().split()]
print('Case {}: {}'.format(t, min(k, n if m>=n else m + (n-m)//2)))
10 changes: 10 additions & 0 deletions Codeforces/CF101840-GYM-L.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'''
AC

No of matches = No of teams - 1
'''
with open('lazy.in', 'r') as f:
T = int(f.readline())
for i in range(T):
n_teams = int(f.readline())
print('Case {}: {}'.format(i+1, n_teams-1))