4.21 401-最小生成树基础训练 总结
AC_zzh2013 · · 个人记录
总结
T1
思路:
先用贪心的思想,将所有边按边权排序,然后维护并查集判断边连接的两点是否在一起了,如果是,则跳过,否则连边并加上边权
T2
思路:
同上题
错误原因
数组开小了
T3
思路:
首先,我们需要明白一点,就是
代码的主要思想就是找到一个最小的环,记录环上的边就可以了
我用来一个vector来记录边
vector<int>ans;
//st:环起点 now:当前点 f:父亲
bool dfs(int st,int now,int f)
{
if(now==st)
{
return 1;
}
for(枚举now的邻居){
int v=g[now][i].first,w=.second;
if(v==f)continue;
ans.push_back(w);
if(dfs(st,v,now))
{
return 1;
}
else
{
//否则说明这条边不在环上
ans.pop_back();
}
}
}
for(i:1~m)
{
int fx=find(e[i].u),fy=find(e[i].v),w=i;//边权就是i
if(fx==fy)
{
//找到边权之和最小的环
ans.push_back(w);
dfs(u,v,u);
sort(ans.begin(),ans.end());
输出ans的所有数字
return ;
}
fa[fy]=fx;
//建图
g[e[i].u].push_back({e[i].v,w});
再反过来建
}
得到这些代码后,代码就可以敲出来了
完整代码
#include<bits/stdc++.h>
using namespace std;
struct stu
{
int v,w;
};
vector<stu>g[1000005];
vector<int>ans;
int fa[1000005];
int dis[1000005];
int fsd[1000005];
int l[1000005];
int r[1000005];
int w[1000005];
int find(int x)
{
if(fa[x]==x)
{
return x;
}
return fa[x]=find(fa[x]);
}
void unionn(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
{
fa[x]=y;
}
return ;
}
bool dfs(int st,int now,int f)
{
if(now==st)
{
return 1;
}
for(auto e:g[now])
{
int v=e.v;
int w=e.w;
if(v==f)
{
continue;
}
ans.push_back(w);
if(dfs(st,v,now))
{
return 1;
}
else
{
ans.pop_back();
}
}
return 0;
}
int main()
{
// freopen("as03.in","r",stdin);
// freopen("as03.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
ans.clear();
for(int i=1;i<=n;++i)
{
fa[i]=i;
g[i].clear();
}
for(int i=1;i<=m;++i)
{
cin>>l[i]>>r[i];
w[i]=i;
}
bool b=0;
for(int i=1;i<=m;++i)
{
int fx=find(l[i]);
int fy=find(r[i]);
int w=i;
if(fx==fy)
{
ans.push_back(w);
dfs(l[i],r[i],w);
sort(ans.begin(),ans.end());
for(auto e:ans)
{
cout<<e<<' ';
}
cout<<'\n';
b=1;
break;
}
fa[fy]=fx;
g[l[i]].push_back({r[i],w});
g[r[i]].push_back({l[i],w});
}
if(!b)
{
cout<<-1<<'\n';
}
}
return 0;
}
T4
思路:
1.st[i]:统计以i作为根时,这棵里起点的数量
2.ed[i]:统计以i作为根时,这棵里终点的数量
3.当最小生成树合并2棵子树时,左边树中的点就可以与右边树中的点联通了,他们的f(左,右)=w
4.所以可以同时去统计两棵子树能够匹配多少对
最后就能写出代码了
完整代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct stu
{
int l,r,w;
}a[200005];
bool cmp(stu a,stu b)
{
return a.w<b.w;
}
int fa[200005];
int st[200005];
int ed[200005];
int find(int x)
{
if(fa[x]==x)
{
return x;
}
return fa[x]=find(fa[x]);
}
void unionn(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
{
fa[x]=y;
}
return ;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=n;++i)
{
fa[i]=i;
}
for(int i=1;i<=m;++i)
{
cin>>a[i].l>>a[i].r>>a[i].w;
}
for(int i=1;i<=k;++i)
{
int x;
cin>>x;
st[x]++;
}
for(int i=1;i<=k;++i)
{
int x;
cin>>x;
ed[x]++;
}
sort(a+1,a+m+1,cmp);
int ans=0;
for(int i=1;i<=m;++i)
{
int u=a[i].l;
int v=a[i].r;
int w=a[i].w;
int fx=find(u);
int fy=find(v);
if(fx==fy)
{
continue;
}
int min1=min(st[fx],ed[fy]);
int min2=min(st[fy],ed[fx]);
ans+=min1*w;
ans+=min2*w;
fa[fy]=fx;
st[fx]-=min1;
ed[fy]-=min1;
st[fx]-=min2;
ed[fy]-=min2;
st[fx]+=st[fy];
ed[fx]+=ed[fy];
}
cout<<ans;
return 0;
}