E
这道题和打击犯罪(black)一个逻辑,删除就是添加。
注意!减少一个点,不是去掉与它连接的边,是删除这一个点。
代码
大部分是注释和调试,代码其实很简短
#include<bits/stdc++.h>
#define debug false
#define int long long
using namespace std;
//namespace rll { //Reduced linked list
// template<typename T, const unsigned int MAX_LENGTH>
// class stack
// {
// public:
// unsigned int t = 0;
// T s[MAX_LENGTH];
// const T& operator[](unsigned int x) const
// {
// if (x >= t)
// exit(11);
// return s[x];
// }
// T& operator[](unsigned int x)
// {
// if (x >= t)
// exit(11);
// return s[x];
// }
// void push(T x)
// {
// s[t++] = x;
// return;
// }
// T pop()
// {
// return s[--t];
// }
// T top()
// {
// return s[t - 1];
// }
// T find(unsigned int x)
// {
// if (x >= t)
// exit(11);
// return s[t - 1 - x];
// }
// void clear()
// {
// delete this;
// }
// ~stack()
// {
// delete this;
// }
// };
int n, m, fa[400005], b[400005], k, ans[400005], tot;
bool vis[400005];
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, tot--;
return;
}
vector<int>a[400005];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
fa[i] = i, vis[i] = true;
for (int i = 1; i <= m; i++)
{
int x, y;
cin >> x >> y;
x++, y++;
a[x].push_back(y);
a[y].push_back(x);
}
cin >> k;
for (int i = 1; i <= k; i++)
{
cin >> b[i];
b[i]++;
vis[b[i]] = false;
}
tot = n;
for (int i = 1; i <= n; i++)
for (auto j : a[i])
if (vis[j] && vis[i])
unionn(j, i);
for (int i = k; i >= 1; i--)
{
ans[i] = tot;
vis[b[i]] = true;
for (auto j : a[b[i]])
if (vis[j])
unionn(b[i], j);
}
cout << tot << "\n";
for (int i = 1; i <= k; i++)
cout << ans[i] - i << '\n';
return 0;
}F P4047 [JSOI2010] 部落划分
并查集+最小生成树
代码
#include<bits/stdc++.h>
#define debug false
#define int long long
using namespace std;
//namespace rll { //Reduced linked list
// template<typename T, const unsigned int MAX_LENGTH>
// class stack
// {
// public:
// unsigned int t = 0;
// T s[MAX_LENGTH];
// const T& operator[](unsigned int x) const
// {
// if (x >= t)
// exit(11);
// return s[x];
// }
// T& operator[](unsigned int x)
// {
// if (x >= t)
// exit(11);
// return s[x];
// }
// void push(T x)
// {
// s[t++] = x;
// return;
// }
// T pop()
// {
// return s[--t];
// }
// T top()
// {
// return s[t - 1];
// }
// T find(unsigned int x)
// {
// if (x >= t)
// exit(11);
// return s[t - 1 - x];
// }
// void clear()
// {
// delete this;
// }
// ~stack()
// {
// delete this;
// }
// };
int n, fa[1005], k, ans, cnt, tot, m[1005][1005];
struct node {
int x, y;
double z;
}a[1000005], b[1005];
bool cmp(node x, node y)
{
return x.z < y.z;
}
//----start----模板
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;
}
//----end----
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++)
fa[i] = i, cin >> b[i].x >> b[i].y;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
{
a[++tot] = node{ i,j,sqrt(0.00 + (b[i].x - b[j].x) * (b[i].x - b[j].x) + (b[i].y - b[j].y) * (b[i].y - b[j].y)) };
m[i][j] = tot;
}
cnt = -1;
sort(a + 1, a + 1 + tot, cmp);
for (int i = 1; i <= tot && cnt + k < n; i++)
if (find(a[i].x) != find(a[i].y))
{
unionn(a[i].x, a[i].y);
cnt++;
if (n - cnt <= k)
{
#if debug
cout << "\tcnt=" << cnt << "\ti=" << i << "\n";
#endif
printf("%.2f", a[i].z);
break;
}
}
#if debug
cout << '\n';
for (int i = 1; i <= 4; i++)
cout << "----";
cout << "debug.fai";
for (int i = 1; i <= 4; i++)
cout << "----";
cout << '\n';
for (int i = 1; i <= n; i++)
cout << '\t' << i << '\t' << find(i) << '\n';
for (int i = 1; i <= 4; i++)
cout << "----";
cout << "debug.x.y";
for (int i = 1; i <= 4; i++)
cout << "----";
cout << '\n';
for (int i = 1; i <= n; i++)
cout << '\t' << i << '\t' << b[i].x << '\t' << b[i].y << '\n';
for (int i = 1; i <= 4; i++)
cout << "----";
cout << " debug.z ";
for (int i = 1; i <= 4; i++)
cout << "----";
cout << '\n';
for (int i = 1; i <= tot; i++)
cout << '\t' << a[i].x << '\t' << a[i].y << '\t' << a[i].z << '\n';
for (int i = 1; i <= 4; i++)
cout << "----";
cout << " debug.n ";
for (int i = 1; i <= 4; i++)
cout << "----";
cout << "\n";
cout << n << " " << k << " " << cnt << " " << n - cnt << "\n";
#endif
return 0;
}G P1265 公路修建
Prim算法
- Prim 算法流程:
- 开始将起点标记;
- 找一条连接被标记的点集合中一点和没有被标记的点集合中一点最短的边;
- 将该边连接的没有被标记的点加入被标记的点;
- 将该新加入的被标记的点所有连接的没有被标记的点最短边更新;
- 返回第二步,直到 个点都被选入最小生成树为止。
代码
#include<bits/stdc++.h>
#define debug false
typedef int iint;//其实根本没用
#define int long long
using namespace std;
//namespace rll { //Reduced linked list
// template<typename T, const unsigned int MAX_LENGTH>
// class stack
// {
// public:
// unsigned int t = 0;
// T s[MAX_LENGTH];
// const T& operator[](unsigned int x) const
// {
// if (x >= t)
// exit(11);
// return s[x];
// }
// T& operator[](unsigned int x)
// {
// if (x >= t)
// exit(11);
// return s[x];
// }
// void push(T x)
// {
// s[t++] = x;
// return;
// }
// T pop()
// {
// return s[--t];
// }
// T top()
// {
// return s[t - 1];
// }
// T find(unsigned int x)
// {
// if (x >= t)
// exit(11);
// return s[t - 1 - x];
// }
// void clear()
// {
// delete this;
// }
// ~stack()
// {
// delete this;
// }
// };
int n/*, fa[5005]*/;//fa:并查集的父节点数组,在这里没用
struct node {
double x, y;
}a[5005];
double dis[5005], ans;
bool vis[5005];
//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;
//}
//获取dis
double get_dis(double x1, double y1, double x2, double y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
//fa[i] = i;//并查集初始化,在这里没用
dis[i] = 1e12;
cin >> a[i].x >> a[i].y;
}
vis[1] = true;
dis[1] = 0.00;
for (int i = 1; i <= n; i++)
{
int curr = 1;
double mini = 1e9;
for (int j = 1; j <= n; j++)
if (!vis[j] && dis[j] < mini)
curr = j, mini = dis[j];
ans += dis[curr];
vis[curr] = true;
for (int j = 1; j <= n; j++)
dis[j] = min(dis[j], get_dis(a[curr].x, a[curr].y, a[j].x, a[j].y));
}
printf("%.2lf", ans);
return 0;
}![[AHOI2002] Kitty猫基因编码、[USACO10OCT] Lake Counting S、海战、[NOIP2001 提高组] 数的划分、[JSOI2008] 星球大战、[JSOI2010] 部落划分、公路修建](https://cdn.luogu.com.cn/upload/image_hosting/nccnxibj.png)