5.6 周二下午训练 — 东塘407
P1195
最小生成树模版 打成 直接原地破防
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 10005;
int n, m, k, ans;
int fa[4 * N];
struct edge{
int x, y, z;
}a[N];
bool cmp(edge aa, edge bb)
{
return aa.z < bb.z;
}
int get(int x)
{
if (fa[x] == x)
return x;
return fa[x] = get(fa[x]);
}
void unite(int x, int y)
{
x = get(x);
y = get(y);
if (x != y)
fa[x] = y;
return ;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= m; i++)
cin >> a[i].x >> a[i].y >> a[i].z;
sort(a + 1, a + m + 1, cmp);
int cnt = n;
for (int i = 1; i <= m; i++)
{
int f1 = get(a[i].x), f2 = get(a[i].y);
if (f1 != f2)
{
unite(f1, f2);
ans += a[i].z;
cnt--;
if (cnt == k)
{
cout << ans;
return 0;
}
}
}
cout << "No Answer";
return 0;
}P1609
有几种情况:
- ABCDDCBA > ABCDEFGH, 直接反过来即可
- 奇数个,把中间的数升位,把ABCDtEFGH变成ABCD(t+1)DCBA
- 偶数个,变成ABC(D+1)(D+1)CBA,当然要特判全9的情况
阿巴阿巴阿巴,反正就是模拟
最讨厌大模拟了
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
string s;
signed main()
{
cin>>s;
int len=s.size();
string l=s.substr(0,len/2), r;
if(len%2==0)
r=s.substr(len/2);
else
r=s.substr(len/2+1);
string fl=l;
reverse(fl.begin(),fl.end());
if(l+fl>l+r)
{
if(len%2==0)
cout<<l+fl;
else
cout<<l<<s[len/2]<<fl;
}
else
{
if(len%2==1&&s[len/2]<'9')
{
s[len/2]++;
cout<<l<<s[len/2]<<fl;
}
else
{
s[len/2]='0';
bool flag=0;
for(int i=l.size()-1;i>=0;i--)
{
if(l[i]<='8')
{
l[i]++;
flag=1;
break;
}
else l[i]='0';
}
if(flag==0)
{
cout<<1;
for(int i=1;i<=s.size()-1;i++)
cout<<0;
cout<<1;
}
else
{
fl=l;
reverse(fl.begin(),fl.end());
if(len%2==0)
cout<<l+fl;
else
cout<<l<<s[len/2]<<fl;
}
}
}
return 0;
}P11486
点、 条点对。要求构造一棵树,所有的 到 的路径,正好把所有的树边全部走了一次(不能多也 不能少)。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 5;
int x[N] , y[N];
int fa[N];
int get(int x)
{
if (x == fa[x])
return x;
return fa[x] = get(fa[x]);
}
void unite(int x , int y)
{
x = get(x);
y = get(y);
if (x != y)
fa[x] = y;
return ;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n , m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1; i <= m; i++)
cin >> x[i] >> y[i];
for (int i = 1; i <= m; i++)
{
int nx = get(x[i]);
int ny = get(y[i]);
if (nx != ny)
{
unite(nx , ny);
}
else
{
cout << "No";
return 0;
}
}
cout << "Yes" << endl;
for (int i = 1; i <= m - 1; i++)
cout << x[i] << " " << y[i] << endl;
int pre = x[m];
for (int i = 1; i <= n; i++)
if (get(i) == i && get(i) != get(x[m]))
{
cout << pre << " " << i << endl;
pre = i;
}
cout << pre << " " << y[m];
return 0;
}