problem1 link

依次枚举每个元素$x$,作为$S$中开始选择的第一个元素。对于当前$S$中任意两个元素$i,j$,若$T[i][j]$不在$S$中,则将其加入$S$,然后继续扩展;若所有的$T[i][j]$都在$S$中,则结束扩展。每次扩展结束之后保存$|S|$的最小值。

problem2 link

总的思路是搜索,分别枚举每一条边是在哪个集合中,进行如下的优化:

(1)使用并查集,当其中两个集合都联通时结束;当有一个集合联通时,直接判断剩下所有的边加入另一个集合能否使得另一个集合联通;

(2)如果当前剩下所有的边都加入到其中一个集合都不能使其联通时,结束搜索返回;

(3)当前边加入第一个集合使其联通分量减少时才进行加入的操作,否则不再继续搜索下去,而将其直接加入另一个集合。

problem3 link

随即生成1000个点数为12个图,然后计算最小生成树的个数。假设可以从中选出四个(可能是相同的)然后串联起来,那么答案就是$A_{1}*A_{2}*A_{3}*A_{4}$。$A_{i}$为选出的第$i$个图的最小生成树的个数。

code for problem1

#include <algorithm>
#include <vector> class MultiplicationTable2 {
public:
int minimalGoodSet(const std::vector<int> &a) {
int n2 = static_cast<int>(a.size());
int n = 1;
while (n * n != n2) {
++n;
}
std::vector<std::vector<int>> g(n, std::vector<int>(n));
auto Compute = [&](int x) {
if (g[x][x] == x) {
return 1;
}
std::vector<int> s;
std::vector<bool> h(n);
s.push_back(x);
h[x] = true;
while (true) {
bool ok = true;
std::vector<int> ns = s;
for (size_t i = 0; i < s.size(); ++i) {
for (size_t j = 0; j < s.size(); ++j) {
int k = g[s[i]][s[j]];
if (!h[k]) {
h[k] = true;
ns.push_back(k);
ok = false;
}
}
}
if (ok) {
break;
}
s = ns;
}
return static_cast<int>(s.size());
};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
g[i][j] = a[i * n + j];
}
}
int result = n;
for (int i = 0; i < n; ++i) {
result = std::min(result, Compute(i));
}
return result;
}
};

code for problem2

#include <string>
#include <vector> constexpr int kMaxN = 10; struct UnionSet {
int a[kMaxN];
int cnt;
int n; void Init(int n) {
this->n = n;
for (int i = 0; i < n; ++i) {
a[i] = i;
}
cnt = 0;
} int Get(int x) {
if (a[x] != x) {
a[x] = Get(a[x]);
}
return a[x];
} bool Update(int x, int y) {
x = Get(x);
y = Get(y);
if (x == y) {
return false;
}
if (x < y) {
a[y] = x;
} else {
a[x] = y;
}
++cnt;
return true;
}
bool OK() { return cnt == n - 1; }
}; int n, m;
std::vector<int> a, b; bool Check(UnionSet s, int id) {
if (s.OK()) {
return 1;
}
if (id >= m) {
return 0;
}
for (int i = id; i < m && !s.OK(); ++i) {
s.Update(a[i], b[i]);
}
return s.OK();
} bool Dfs(int id, UnionSet s1, UnionSet s2) {
if (s1.OK() && s2.OK()) {
return 1;
}
if (s1.OK()) {
return Check(s2, id);
}
if (s2.OK()) {
return Check(s1, id);
} if (!Check(s1, id) || !Check(s2, id)) {
return false;
} if (id >= m) {
return false;
} while (id < m) {
if (s1.Get(a[id]) != s1.Get(b[id])) {
UnionSet new_s1 = s1;
new_s1.Update(a[id], b[id]);
if (Dfs(id + 1, new_s1, s2)) {
return 1;
}
}
s2.Update(a[id], b[id]);
++id;
}
return false;
} class FoxAirline2 {
public:
std::string isPossible(int node_number, const std::vector<int> &ea,
const std::vector<int> &eb) {
n = node_number;
a = ea;
b = eb;
m = static_cast<int>(a.size()); UnionSet s1, s2;
s1.Init(n);
s2.Init(n);
if (Dfs(0, s1, s2)) {
return "Possible";
}
return "Impossible";
}
};

code for problem3

#include <cstdlib>
#include <ctime>
#include <unordered_map>
#include <vector> class MSTCounter {
public:
static int Pow(long long a, int b, int mod) {
long long result = 1;
while (b > 0) {
if (b % 2 == 1) {
result = result * a % mod;
}
a = a * a % mod;
b /= 2;
}
return static_cast<int>(result);
}
static int Solver(const std::vector<std::vector<bool>> &g, int mod) {
int n = static_cast<int>(g.size());
std::vector<std::vector<int>> a(n, std::vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j && g[i][j]) {
a[i][j] = mod - 1;
a[i][i] += 1;
}
}
}
bool tag = false;
for (int i = 1; i < n; ++i) {
int k = 0;
for (int j = i; j < n; ++j) {
if (a[j][i] != 0) {
k = j;
break;
}
}
if (i != k) {
tag = !tag;
std::swap(a[i], a[k]);
}
for (int j = i + 1; j < n; ++j) {
long long t = 1ll * a[j][i] * Pow(a[i][i], mod - 2, mod) % mod;
for (int k = i; k < n; ++k) {
a[j][k] = static_cast<int>(mod - t * a[i][k] % mod + a[j][k]) % mod;
}
}
}
long long result = 1;
for (int i = 1; i < n; ++i) {
result = result * a[i][i] % mod;
}
if (tag) {
result = mod - result;
}
return static_cast<int>(result);
}
}; static constexpr int kMod = 1000000007;
static constexpr int kNode = 12;
static constexpr int kSampleNumber = 1024; struct Graph {
std::vector<std::vector<bool>> g;
int result; void Construct() {
g.resize(kNode);
for (int i = 0; i < kNode; ++i) {
g[i].resize(kNode);
}
for (int i = 0; i < kNode; ++i) {
for (int j = i + 1; j < kNode; ++j) {
bool t = (std::rand() & 1) == 1;
g[i][j] = g[j][i] = t;
}
}
result = MSTCounter::Solver(g, kMod);
}
void GetResult(int n, int start, std::vector<int> *result) {
for (int i = 0; i < kNode; ++i) {
for (int j = i + 1; j < kNode; ++j) {
if (g[i][j]) {
int u = i + start;
int v = j + start;
result->push_back(u * n + v);
}
}
}
}
}; Graph graph[kSampleNumber]; class InverseMatrixTree {
public:
std::vector<int> constructGraph(int r) {
if (r == 0) {
return {2};
}
std::srand(std::time(nullptr));
for (int i = 0; i < kSampleNumber; ++i) {
graph[i].Construct();
}
std::unordered_map<int, std::pair<int, int>> mapper;
for (int i = 0; i < kSampleNumber; ++i) {
for (int j = i; j < kSampleNumber; ++j) {
int key =
static_cast<int>(1ll * graph[i].result * graph[j].result % kMod);
if (key != 0) {
mapper[key] = {i, j};
}
}
}
for (const auto &e : mapper) {
int key0 = e.first;
int key1 = static_cast<int>(1ll * r *
MSTCounter::Pow(key0, kMod - 2, kMod) % kMod);
if (mapper.count(key1)) {
int t[4] = {e.second.first, e.second.second, mapper[key1].first,
mapper[key1].second};
int n = kNode * 4;
std::vector<int> result = {n};
for (int i = 0; i < 4; ++i) {
graph[t[i]].GetResult(n, kNode * i, &result);
if (i > 0) {
int u = kNode * i;
int v = u - 1;
result.push_back(u * n + v);
}
}
return result;
}
}
return {};
}
};

参考

http://uoj.ac/problem/75

http://vfleaking.blog.uoj.ac/blog/180

topcoder srm 685 div1的更多相关文章

  1. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  2. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  3. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  4. topcoder srm 738 div1 FindThePerfectTriangle(枚举)

    Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle wi ...

  5. Topcoder SRM 602 div1题解

    打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...

  6. Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

    Problem Statement      The Happy Letter game is played as follows: At the beginning, several players ...

  7. Topcoder SRM 584 DIV1 600

    思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...

  8. TopCoder SRM 605 DIV1

    604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...

  9. topcoder srm 575 div1

    problem1 link 如果$k$是先手必胜那么$f(k)=1$否则$f(k)=0$ 通过对前面小的数字的计算可以发现:(1)$f(2k+1)=0$,(2)$f(2^{2k+1})=0$,(3)其 ...

随机推荐

  1. node.js初识12

    1.express框架属于后端的框架 cnpm install --save express --save的作用是将下载的保存在package.json中 你可以点击http://www.expres ...

  2. 安装FusionInsight

    1.在华为平台上下载整体客户端,不建议下载单个组件客户端,后期关联测试还是要装上的.   2.下载后需要将服务器上的客户端拷贝到本地.打开xShell,新建会话,登陆本地虚拟机上的Linux系统(19 ...

  3. python时间和日期

    一.time 和 calendar 模块可以用于格式化日期和时间 import time; # 引入time模块 ticks = time.time() print "当前时间戳为:&quo ...

  4. Mongodb 文档时间字段修改

    mongo文档[tblEvent]如下: {     "_id" : ObjectId("5a0415f9bf28b684b1c7f5b2"),     &qu ...

  5. RDD、DataFrame、Dataset三者三者之间转换

    转化: RDD.DataFrame.Dataset三者有许多共性,有各自适用的场景常常需要在三者之间转换 DataFrame/Dataset转RDD: 这个转换很简单 val rdd1=testDF. ...

  6. java字符串转换总结

    1.byte[]转String String str = new String(strByte); 2.String转byte[] byte[] byteArr = str.getBytes(); 3 ...

  7. ps 证件照(1,2寸)

    制作证件照      9*9打印 1,1寸  图片裁剪 2, 2寸 图片裁剪 3,将裁剪完成后的图片选择添加画布  Alt Ctrl  c 将高和宽各加20px  ,背景选择白色 4,将得到的带有白色 ...

  8. sql2008升级到r2提示:检查当前是否正确配置了报表服务器、数据库服务器是否正在运行以及您是否有权访问

    sql2008升级到r2提示:检查当前是否正确配置了报表服务器.数据库服务器是否正在运行以及您是否有权访问 解决方法:把服务开启ok

  9. MyBatis学习笔记(一)——MyBatis快速入门

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4261895.html 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优 ...

  10. JustOj 2043: N!

    题目描述 输出N的阶乘.(注意时间限制150ms&&注意不能打表后输出,赛后我们会检查代码,如有发现,该位同学总分记0分处理) 打表的定义:在本地主机预先计算出了每个值对应的答案,并把 ...