P1636 Einstein学画画
题目描述
Einstein 学起了画画。
此人比较懒~~,他希望用最少的笔画画出一张画……
给定一个无向图,包含 nn 个顶点(编号 1 \sim n1∼n),mm 条边,求最少用多少笔可以画出图中所有的边。
输入格式
第一行两个整数 n, mn,m。
接下来 mm 行,每行两个数 a, ba,b(a \ne ba=b),表示 a, ba,b 两点之间有一条边相连。
一条边不会被描述多次。
输出格式
一个数,即问题的答案。
输入输出样例
输入 #1
5 5 2 3 2 4 2 5 3 4 4 5
输出 #1
1
说明/提示
对于 50 \%50% 的数据,n \le 50n≤50,m \le 100m≤100。
对于 100\%100% 的数据,1 \le n \le 10001≤n≤1000,1 \le m \le {10}^51≤m≤105。
题解:
统计所有点的度数,如果是奇数结果加一,如果是偶数则是途中的点,最后结果除以二,不过需要注意连成一串的点,此时所有点的度都是偶数,但是可以一笔画完。
#include<bits/stdc++.h>
using namespace std;
int n, m, cnt[1005], ans;
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
cnt[x]++;
cnt[y]++;
}
for (int i = 1; i <= n; i++) {
if (cnt[i]%2!=0) {
ans++;
}
}
if (ans == 0) {
cout << 1;
}
else {
cout << ans / 2;
}
system("pause");
return 0;
}
P8654 [蓝桥杯 2017 国 C] 合根植物
题目描述
w 星球的一个种植园,被分成 m \times nm×n 个小格子(东西方向 mm 行,南北方向 nn 列)。每个格子里种了一株合根植物。
这种植物有个特点,它的根可能会沿着南北或东西方向伸展,从而与另一个格子的植物合成为一体。
如果我们告诉你哪些小格子间出现了连根现象,你能说出这个园中一共有多少株合根植物吗?
输入格式
第一行,两个整数 mm,nn,用空格分开,表示格子的行数、列数(1<m,n<10001<m,n<1000)。
接下来一行,一个整数 kk,表示下面还有 kk 行数据 (0<k<10^5)(0<k<105)。
接下来 kk 行,第行两个整数 aa,bb,表示编号为 aa 的小格子和编号为 bb 的小格子合根了。
格子的编号一行一行,从上到下,从左到右编号。
比如:5 \times 45×4 的小格子,编号:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
输出格式
一行一个整数,表示答案
输入输出样例
输入 #1
5 4 16 2 3 1 5 5 9 4 8 7 8 9 10 10 11 11 12 10 14 12 16 14 18 17 18 15 19 19 20 9 13 13 17
输出 #1
5
说明/提示
样例解释
时限 1 秒, 256M。蓝桥杯 2017 年第八届国赛
题解:
先将所有点分别设为父节点之后合并,用find函数确定点的父节点,因两个点在合并后,后一个点又和其他点合并,所以全部合并完之后需要全部find一遍,最后求出父节点的数量。
#include<bits/stdc++.h>
using namespace std;
long long n, m, k;
long long ans;
vector<long long>jihe;
vector<long long>vis;
int find(long long x)
{
if (jihe[x] == x) return x;
else
{
jihe[x] = find(jihe[x]);
return jihe[x];
}
}
void hb(long long i, long long j)
{
jihe[find(i)] = find(j);
}
int main()
{
cin >> n >> m >> k;
jihe.resize(m * n + 1);
vis.resize(m * n + 1);
for (int i = 1; i <= m * n; i++) {
jihe[i] = i;
}
for (int i = 1; i <= k; i++)
{
int u, v;
cin >> u >> v;
hb(u, v);
}
for (int i = 1; i <= m * n; i++) {
find(i);
}
for (int i = 1; i <= n * m; i++)
{
if (vis[jihe[i]] == 1) continue;
vis[jihe[i]] = 1;
ans++;
}
cout << ans << endl;
return 0;
}
P3958 [NOIP2017 提高组] 奶酪
题解:
建立一个判断函数和一个dfs函数,如果两个洞能连接,则给后一个洞设置为已遍历,接着往下走,直到结束,设置jud记录结果。
#include<bits/stdc++.h>
using namespace std;
unsigned long long r, n, h, jud;
unsigned long long m[1005];
struct dong {
double x, y, z;
};
dong p[1005];
bool pd(dong a, dong b) {
long long d;
d = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z);
if (d <= 4 * r * r) return true;
else return false;
}
void run(int x) {
if (jud == 1) return;
if (p[x].z + r >= h) {
jud = 1;
return;
}
for (int i = 1; i <= n; i++) {
if (m[i] == 1) continue;
if (pd(p[x], p[i])) {
m[i] = 1;
run(i);
}
}
}
int main() {
int T;
cin >> T;
for (int j = 1; j <= T; j++) {
cin >> n >> h >> r;
jud = 0;
for (int i = 1; i <= n; i++) {
m[i] = 0;
}
for (int i = 1; i <= n; i++) {
cin >> p[i].x >> p[i].y >> p[i].z;
}
for (int i = 1; i <= n; i++)
if (p[i].z <= r) {
m[i] = 1;
run(i);
}
if (jud == 1) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
return 0;
}
P1119 灾后重建
题解:
核心思路为Floyd思想
#include<bits/stdc++.h>
using namespace std;
int n, m;
int a[205];
int f[205][205];
inline void gx(int k) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (f[i][j] > f[i][k] + f[j][k])
f[i][j] = f[j][i] = f[i][k] + f[j][k];
return;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
f[i][j] = 1e9;
}
for (int i = 0; i < n; i++)
f[i][i] = 0;
int s1, s2, s3;
for (int i = 1; i <= m; i++) {
cin >> s1 >> s2 >> s3;
f[s1][s2] = f[s2][s1] = s3;
}
int q;
cin >> q;
int now = 0;
for (int i = 1; i <= q; i++) {
cin >> s1 >> s2 >> s3;
while (a[now] <= s3 && now < n) {
gx(now);
now++;
}
if (a[s1] > s3 || a[s2] > s3)cout << -1 << endl;
else {
if (f[s1][s2] == 1e9)cout << -1 << endl;
else cout << f[s1][s2] << endl;
}
}
return 0;
}
P2504 [HAOI2006]聪明的猴子
题解:
使用kruskal算法,比较边和猴子跳跃距离,如果能跳过去就结果++;
#include<bits/stdc++.h>
using namespace std;
int n, m, k, ans = 0;
double sum = -1;
int a[1000005][3], b[1000005], pre[1000005];
struct zzz {
int x, y;
double p;
} z[1000005];
int cmp(zzz k, zzz d) {
return k.p < d.p;
}
int find(int x) {
if (pre[x] == x)
return x;
return pre[x] = find(pre[x]);
}
int main() {
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> b[i];
}
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i][1] >> a[i][2];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j) {
k++;
z[k].x = i;
z[k].y = j;
z[k].p = sqrt((a[i][1] - a[j][1]) * (a[i][1] - a[j][1]) + (a[i][2] - a[j][2]) * (a[i][2] - a[j][2]));
}
}
}
int cnt = n;
sort(z + 1, z + k + 1, cmp);
for (int i = 1; i <= n; i++) {
pre[i] = i;
}
for (int i = 1; i <= k; i++) {
if (cnt == 1)
break;
int s1 = find(z[i].x), s2 = find(z[i].y);
if (s1 != s2) {
pre[s1] = s2;
cnt--;
sum = z[i].p;
}
}
for (int i = 1; i <= m; i++) {
if (sum <= b[i])
ans++;
}
cout << ans << endl;
return 0;
}