HDU1102 Constructing Roads:比较水,只要把修建的道路置为零就可以
计蒜客 灌溉:比上一题还水,直接上手就可以
2018.4.14
HDU1213 how many tables:一道并查集的题目,不难,但是出了篓子
#include<iostream>
#include<algorithm>
using namespace std;
int p[1007];
int found(int x)
{
return p[x] == x ? x : p[x] = found(p[x]);
}
int main()
{
int t,m,n;
int a,b;
cin >> t;
while(t--)
{
cin >> n >> m;
int ans = n;
for(int i = 1; i <= n; i ++)
{
p[i] = i;
}
for(int i = 0; i < m; i ++)
{
cin >> a >> b;
int x = found(a);
int y = found(b);
if(x != y)
{
p[x] = y;
ans --;//错误更改为此处
}
}
// —————————错误部分————————————
// for(int i = 1; i <= n; i ++)
// found(i);
// int ans = 1;
// sort(p+1,p+1+n);
// for(int i = 1; i < n; i ++)
// if(p[i]!= p[i+1])ans++;
// —————————错误部分————————————
cout << ans << endl;
}
return 0;
}
HDU1232 畅通工程 水题 最小生成树
2018.4.15
HDU1233 还是畅通工程 水题 最小生成树
HDU1863 畅通工程 不是太难,注意条件
HDU1875 畅通工程再续 注意条件
HDU1879 继续畅通工程 这个题使用cin cout来输入输出超时了,改成scanf printf就过了
2018.4.16
HDU3371 connect the cities 从这道题开始我要相信玄学了,TLE了无数次,然后在把cmp的const int 改成int之后就AC了?
贴代码
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = 25002;
int u[maxn],v[maxn],w[maxn],r[maxn],p[maxn];
bool cmp(int i, int j)//就是这里的锅←
{
return w[i] < w[j];
}
int found(int x)
{
return p[x] == x ? x : p[x] = found(p[x]);
}
int main()
{
int n,k,m;
int cases,t;
scanf("%d",&cases);
while(cases --)
{
int counts = 1;
int ans = 0;
scanf("%d%d%d",&n,&m,&k);
for(int i = 0; i < m; i ++)
{
scanf("%d%d%d",&u[i],&v[i],&w[i]);
}
for(int i = 0; i <= max(m,n); i ++)
{
p[i] = i;
r[i] = i;
}
int temp1,temp2;
for(int i = 0; i < k; i ++)
{
scanf("%d%d",&t,&temp1);
t--;
while(t--)
{
scanf("%d",&temp2);
int x = found(temp1);
int y = found(temp2);
if(x != y)
{
p[x] = y;
counts++;
}
temp1 = temp2;
}
}
sort(r,r+m,cmp);
for(int i = 0; i < m; i ++)
{
int e = r[i];
int x = found(u[e]);
int y = found(v[e]);
if(x != y)
{
p[x] = y;
ans += w[e];
counts ++;
}
}
counts == n ? printf("%d\n",ans) : printf("-1\n");
}
return 0;
}
一道atcoder的题目,学到了一点骚操作,单独写了个博客,链接