一条边的时候会直接变黑 所以就是求删成树边的需要的总边数 并查集维护连通性 注意自环和重边
#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认为一个整数序列是好的,当且仅当它先(不严格)上升,后(不严格)下降。形式化地,我们认为序是好的,当且仅当存在某个 ,使得对于任意,有;且对于任意,有 小Rintarou得到了一个长度为 的序列,他想让这个序列变成好的。小Rintarou每次可以交换相邻的两个元素。因为交换很累,所以小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
一共有个笼子,编号为到,每个笼子里关了一个生物(上面三个之一),由于他们不想让别人看到笼子里的生物,所以用布挡了起来,那么就假装笼子里的三种生物以相同的概率出现,即一共有种可能的初始状态。 他们一共进行了次操作。 操作有两种 . 把第 个笼子拆掉,并把第个笼子里的生物关到了第个笼子里。两个生物在同一个笼子里会打架,胜者会活下来。如果种类相同则原来在 中的生物会活下来(主场优势,输的会被啊掉) .Rintarou问Kurisu有多少种初始状态使得原来在第 个笼子里的生物现在还活着。 Kurisu觉得这道问题太水了不屑于做,于是把他交给了你。
带权并查集 发现不管这个位置是什么 随机后赢得概率是2/3 输的概率是1/3
所以每次合并 给u集合2/3 v集合1/3 注意标记永久化 即和到父亲上时要先除到父亲的值 这样一路向上得答案的时候就会消除父亲的影响因为同一时刻合并的两个时互不影响的 只有两个合并之后再和别的合并才会有影响 保证父亲合并之前的不影响儿子
#include <bits/stdc++.h>
#define ll long long
const int mod = 998244353;
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 n, m;
int fa[N], dep[N];
inline int find(int x)
{
if (fa[x] == x)
return x;
return find(fa[x]);
}
ll inv2, inv3;
ll now[N];
ll ksm(ll a, ll b)
{
ll res = 1, base = a;
while (b)
{
if (b & 1)
res = res * base % mod;
base = base * base % mod;
b >>= 1;
}
return res;
}
ll ans1;
int main()
{
read(n);
read(m);
ans1 = 1;
for (int i = 1; i <= n; ++i)
fa[i] = i, now[i] = 1, ans1 = ans1 * 3 % mod, dep[i] = 1;
inv2 = ksm(2, mod - 2);
inv3 = ksm(3, mod - 2);
for (int i = 1, x, y; i <= m; ++i)
{
int opt;
read(opt);
if (opt == 1)
{
read(x), read(y);
x = find(x), y = find(y);
if (dep[x] < dep[y])
{
now[x] = (now[x] * 2ll % mod * ksm(now[y], mod - 2) % mod) % mod;
now[y] = (now[y] * inv3) % mod;
fa[x] = y;
}
else
{
if (dep[x] == dep[y])
{
dep[x]++;
}
fa[y] = x;
now[y] = (now[y] * inv2 % mod * ksm(now[x], mod - 2)) % mod;
now[x] = (now[x] * 2ll % mod * inv3) % mod;
}
}
else
{
ll ans = ans1;
read(x);
while (x != fa[x])
{
ans = (ans * now[x]) % mod;
x = fa[x];
}
ans = (ans * now[x]) % mod;
printf("%lld\n", ans);
}
}
return 0;
}