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. 使用Spark下的corr计算皮尔森相似度Pearson时,报错Can only zip RDDs with same number of elements in each partition....

    package com.huawei.bigdata.spark.examples import org.apache.spark.mllib.stat.Statistics import org.a ...

  2. a标签下载;页面传参row对象先转换成字符串。

    jsp:添加一列 <th data-options="field:'id',width:180,formatter: rowformater" width="20% ...

  3. 合并dict、list的方法

    dict1={1:[1,11,111],2:[2,22,222]}dict2={3:[3,33,333],4:[4,44,444]}合并两个字典得到类似 {1:[1,11,111],2:[2,22,2 ...

  4. 【2017-04-17】类库、通用变量、is和as、委托

    类库dll文件,里边有很多被编译后的C#代码,不可阅读,不可修改,只能调用 1.类库创建 新建项目为类库,类库文件编写完成后,选择生成—生成解决方案,在debug文件夹下找到dll文件 2.类库引用 ...

  5. java踩坑

    1. java判断两个字符串是否相等用equals 2. java只传递指针遇到的坑: 1 import java.util.*; 2 3 public class mapTest { 4 publi ...

  6. SQLServer 创建自己的数据库

    1)进入数据库服务器,创建自己的数据库 use master go create database Dt_Devtest on primary(name=[Dt_new_data],filename= ...

  7. Locust 设置响应断言

    转:http://www.testclass.net/locust/assert/ 性能测试也需要设置断言么? 某些情况下是需要,比如你在请求一个页面时,就可以通过状态来判断返回的 HTTP 状态码是 ...

  8. JAVA中异常状况总结

    之前在<会当凌绝顶>这本书中学到过对于异常处理的知识,当时也是根据书上的代码,自己进行编写大概知道是怎么回事儿,王老师给我们上了一节课之后,发现异常处理可以发挥很大的作用.  通过在网络上 ...

  9. [转载]Oracle数据库 sql%found,sql%notfound,sql%rowcount

    sql%found,sql%notfound,sql%rowcount 在执行DML(insert,update,delete)语句时,可以用到以下三个隐式游标(游标是维护查询结果的内存中的一个区域, ...

  10. P2800 又上锁妖塔

    P2800 又上锁妖塔  [温馨提示]使用仙术时飞过一层塔不花费时间,若爬过去,该层有多高,就要花费多长时间     我们可以用 f [ i ] 表示到达第 i 层时所用最短时间   到达第 i 层可 ...