11.28模拟赛
一条边的时候会直接变黑 所以就是求删成树边的需要的总边数 并查集维护连通性 注意自环和重边
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 20;
int d[N];
int n, m, x, y, fa[N];
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
fa[i] = i;
int tmp = 0, relm = 0;
for (int i = 1; i <= m; i++)
{
scanf("%d%d", &x, &y);
if (x == y)
tmp++;
else
relm++, d[x]++, d[y]++;
fa[find(x)] = find(y);
}
int block = 0, reln = 0;
for (int i = 1; i <= n; i++)
if (d[i] > 0)
reln++;
for (int i = 1; i <= n; i++)
if (i == find(i) && d[i] > 0)
block++;
printf("%d\n", m - reln + block);
return 0;
}
小Rintarou正在研究交换。 小Rintarou认为一个整数序列是好的,当且仅当它先(不严格)上升,后(不严格)下降。形式化地,我们认为序
发现实际上就是把每个点移到左边或右边所有大于他的左边 两者取
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x)
{
x = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
if (f == -1)
x = -x;
}
const int N = 1e6;
int t[N], ans[N];
int n, m;
int a[N], b[N];
inline int lowbit(int x)
{
return x & (-x);
}
inline void add(int x, int y)
{
while (x <= n)
t[x] += y, x += lowbit(x);
}
inline int query(int x)
{
int res = 0;
while (x)
res += t[x], x -= lowbit(x);
return res;
}
int main()
{
//freopen("psy2.in", "r", stdin);
read(n);
for (int i = 1; i <= n; ++i)
{
read(a[i]);
b[i] = a[i];
}
sort(b, b + n + 1);
int q = unique(b, b + 1 + n) - b - 1;
for (int i = 1; i <= n; ++i)
a[i] = lower_bound(b, b + q + 1, a[i]) - b;
for (int i = 1; i <= n; ++i)
{
ans[i] = query(n) - query(a[i]);
add(a[i], 1);
}
memset(t, 0, sizeof(t));
long long res = 0;
for (int i = n; i >= 1; --i)
{
ans[i] = min(ans[i], query(n) - query(a[i]));
add(a[i], 1);
res += ans[i];
}
cout << res << endl;
return 0;
}
“宇宙虽有其起源,却没有终结——无限。 星球虽也有起源,却因其自身之力走向毁灭——有限。 拥有睿智之才是最为愚蠢者,历史上不胜枚举... 这也可以说是给那些抵抗者们的,神的最后通牒。”
Rintarou、Kurisu、Mayuri在玩游戏。 有3种生物,小Rintarou、小Kurisu、和小Mayuri。 小Rintarou能战胜小Mayuri,小Kurisu能战胜小Rintarou,小Mayuri能战胜小Kurisu
一共有