您的位置:首页 > 编程语言

2013编程之美资格赛结束,新手上路,收藏no.1认真学习(from mochavic)

2013-04-12 11:44 465 查看
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
long long f(long long x){
return x * (x - 1) / 2;
}
int main(){
long long ans = 0;
int T, n, m, k, ri = 1, x, y, z;
scanf("%d", &T);
while (T--){
scanf("%d%d%d", &n, &m, &k);
ans = 0;
for (x = 1; x <= n; x++){
y = k / x;
z = k % x;
if (y + (z?1:0) > m) continue;
ans = max(f(x) * f(y) + y * f(z), ans);
}
for (x = 1; x <= m; x++){
y = k / x;
z = k % x;
if (y + (z?1:0) > n) continue;
ans = max(f(x) * f(y) + y * f(z), ans);
}
cout << "Case #" << ri++ << ": " << ans << endl;
}
return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int deep[100010], f[100010][2];
vector<int> e[100010];
int c[60], cn;
void dfs(int fa, int x, int d){
int i, y;
deep[x] = d;
for (i = 0; i < (int)e[x].size(); i += 2){
y = e[x][i];
if (y == fa) continue;
f[y][0] = x;
f[y][1] = e[x][i + 1];
dfs(x, y, d + 1);
}
}
void pd(int x, int y){
while (deep[x] > deep[y]){
c[cn++] = f[x][1];
x = f[x][0];
if (cn == 50) return;
}
while (deep[y] > deep[x]){
c[cn++] = f[y][1];
y = f[y][0];
if (cn == 50) return;
}
while (x != y){
c[cn++] = f[x][1];
x = f[x][0];
c[cn++] = f[y][1];
y = f[y][0];
if (cn >= 50) return;
}
}
int main(){
int T, ri = 1, n, m, x, y, z, i;
scanf("%d", &T);
while (T--){
scanf("%d", &n);
for (i = 1; i <= n; i++) e[i].clear();
for (i = 1; i < n; i++){
scanf("%d%d%d", &x, &y, &z);
e[x].push_back(y);
e[x].push_back(z);
e[y].push_back(x);
e[y].push_back(z);
}
dfs(0, 1, 0);
printf("Case #%d:n", ri++);
scanf("%d", &m);
while (m--){
scanf("%d%d", &x, &y);
cn = 0;
pd(x, y);
sort(c, c + cn);
for (i = 0; i + 2 < cn; i++){
if (c[i] + c[i + 1] > c[i + 2]) break;
}
if (i + 2 < cn) printf("Yesn");
else printf("Non");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: