标签: 图论

[HDU5385]The path 题解

[HDU5385]The path 题解

题目地址:HDUOJ:Problem – 5385 题目描述 You have 

[POI2001]Peaceful Commission 题解 & 2-SAT题目解法

[POI2001]Peaceful Commission 题解 & 2-SAT题目解法

题目地址:HDUOJ:Problem – 1814

题目描述

The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is the fact that some deputies do not get on with some others.
The Commission has to fulfill the following conditions:
1.Each party has exactly one representative in the Commission,
2.If two deputies do not like each other, they cannot both belong to the Commission.
Each party has exactly two deputies in the Parliament. All of them are numbered from 1 to 2n. Deputies with numbers 2i-1 and 2i belong to the i-th party .
Task
Write a program, which:

  1. reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms,
  2. decides whether it is possible to establish the Commission, and if so, proposes the list of members,
  3. writes the result in the text file SPO.OUT.

有n个党派,每个党派2人,其中有m对人有矛盾无法同时出席会议,要求从每个党派选1人出席会议,求一种字典序最小的方案,如果无解,输出NIE。输入包含多组数据。

输入输出格式

输入格式:
In the first line of the text file SPO.IN there are two non-negative integers n and m. They denote respectively: the number of parties, 1 <= n <= 8000, and the number of pairs of deputies, who do not like each other, 0 <= m <=2 0000. In each of the following m lines there is written one pair of integers a and b, 1 <= a < b <= 2n, separated by a single space. It means that the deputies a and b do not like each other.
There are multiple test cases. Process to end of file.

输出格式:
The text file SPO.OUT should contain one word NIE (means NO in Polish), if the setting up of the Commission is impossible. In case when setting up of the Commission is possible the file SPO.OUT should contain n integers from the interval from 1 to 2n, written in the ascending order, indicating numbers of deputies who can form the Commission. Each of these numbers should be written in a separate line. If the Commission can be formed in various ways, your program may write mininum number sequence.

输入输出样例

输入样例#1:

3 2
1 3
2 4

输出样例#1:

1
4
5

题解

本题是2-SAT类题目的裸题,因此下面的讲解也适用于其他2-SAT题目。
对于本题,由于每个党派都是2个人选1个人参加会议,那么如果两个人有矛盾,假设他们是A1和B1,那么如果选择A1则必须选择B2,如果选择B1则必须选择A2(原命题与逆否命题)。我们把这种“必须”给建成图中的有向边。那么就转化为一个图论问题。
对于2-SAT问题的图,我们采取枚举-检验的方式寻找可行解。我们从每一个可能的情况入手,假如这个党派的答案当前没有被确定,那么首先检查选择A1是否可行,如果不可行再检查选择A2(因为本题要求输出字典序最小的解)。对于检查,我们可以使用DFS,如果发现选择A1的这条链上已经存在有被标记“不可选择”的点,那么选择A1是不可行的,因为图中的边都表示“必须”的含义。尝试失败后,要记得将此次尝试涉及的点的状态还原。
另外,当点较多的时候,可以使用Tarjan-SCC缩点后在缩点图上进行上述操作。这种优化方法可能会在之后的题目中讲解。

代码

以下代码借鉴了刘汝佳、陈锋著《算法竞赛入门经典 训练指南》一书的写法。

// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>

#include <vector>

const int MAXN = 16005;

std::vector<int> gra[MAXN], ans;
int n, m, ut, vt;
bool mark[MAXN];

inline bool dfs(int x) {
    if(mark[x ^ 1]) return false;
    if(mark[x]) return true;
    mark[x] = true;
    ans.push_back(x);
    for(int i = 0; i < gra[x].size(); i++) {
        if(!dfs(gra[x][i])) return false;
    }
    return true;
}

inline bool work() {
    memset(mark, 0, sizeof(mark));
    for(int i = 0; i < n << 1; i += 2) {
        if(!mark[i] && !mark[i ^ 1]) {
            ans.clear();
            if(!dfs(i)) {
                for(int j = 0; j < ans.size(); j++) {
                    mark[ans[j]] = false;
                }
                ans.clear();
                if(!dfs(i ^ 1)) return false;
            }
        }
    }
    return true;
}

int main() {
    while(scanf("%d%d", &n, &m) != EOF) {
        for(int i = 0; i < n << 1; i++) {
            gra[i].clear();
        }
        for(int i = 1; i <= m; i++) {
            scanf("%d%d", &ut, &vt); ut--; vt--;
            gra[ut].push_back(vt ^ 1);
            gra[vt].push_back(ut ^ 1);
        }
        if(work()) {
            for(int i = 0; i < n << 1; i += 2) {
                if(mark[i]) printf("%d\n", i + 1);
                else printf("%d\n", (i ^ 1) + 1);
            }
        } else {
            puts("NIE");
        }
    } 
    return 0;
}
[APIO2013]机器人 题解

[APIO2013]机器人 题解

题目地址:洛谷:【P3638】[APIO2013]机器人 – 洛谷、BZOJ: 

[WC2008]游览计划 题解 & 斯坦纳树类题目解法

[WC2008]游览计划 题解 & 斯坦纳树类题目解法

题目地址:洛谷:【P4294】[WC2008]游览计划 – 洛谷、BZOJ:P 

[HDU4903]The only survival 题解

[HDU4903]The only survival 题解

题目地址:HDUOJ:Problem – 4903

题目描述

There is an old country and the king fell in love with a devil. The devil always ask the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.
Something bad actually happen. The devil makes this kingdom’s people infected by a disease called lolicon. Lolicon will take away people’s life in silence.
Although z*p is died, his friend, y*wan is not a lolicon. Y*wan is the only one in the country who is immune of lolicon, because he like the adult one so much.
As this country is going to hell, y*wan want to save this country from lolicon, so he starts his journey.
You heard about it and want to help y*wan, but y*wan questioned your IQ, and give you a question, so you should solve it to prove your IQ is high enough.
The problem is about counting. How many undirected graphs satisfied the following constraints?

  1. This graph is a complete graph of size n.
  2. Every edge has integer cost from 1 to L.
  3. The cost of the shortest path from 1 to n is k.

Can you solve it?
output the answer modulo 10^9+7
有一张n个点的无向完全图,第i个点的编号是i,每条边的边权在1到L之间的正整数,问存在多少个图使得1到n的最短路是k。

输入输出格式

输入格式:
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains 3 integers n,k,L.
T<=5 n,k<=12,L<=10^9.

输出格式:
For each test case, output the answer in one line.

输入输出样例

输入样例#1:

2
3 3 3
4 4 4

输出样例#1:

8
668

题解

首先是方案数,我们就要找方案。考虑如果暴力地解决应该如何,我们可以首先把点1到每个点的最短路长度给枚举出来,然后构造这个图。但是仔细地分析一下,我们似乎不关心点的编号,只关心点的数量。那么这就好办了,我们只需要枚举1到该点最短路长度为定值的点有多少个就好了。
下面的叙述中,用dis代替某点最短路长度。
但是怎么统计方案数呢?考虑当前枚举到dis为x的点有i个的状况,枚举最短路长度1~x-1的方案数早已算出为res,且前面已经枚举过的点数和为tot,cnt数组表示dis为某值的点有多少个。
首先从剩下的点里面把i个点选出来,乘C_{n-tot-1}^i
这些点之间的边是无所谓的,所以每条边随便给个长度就行,乘L^{C_i^2}
dis更小的点向这些点连边,设当前枚举到了之前的dis为x'的情况,这些边要满足x' + w \geq x(j是dis更小的点,i是当前dis的点,w是这条边的边权),每条边的边权有L - (x - x') + 1种可以选择,但是如果全都选择了比x - x'大的边权,最短路长度就无法满足,因此有一部分方案不能满足,要减去,所以最后的方案数是 (L - (x - x') + 1)^{cnt_{x'}} - (L - (x - x'))^{cnt_{x'}}
把上面这一大堆乘进答案里就好啦。
到达枚举终点时tot还有可能比n小,即有点没有算过。这些点内部的边肯定是任意怎么样都行,但是要保证它们的dis比k大,这样的话,上面的“一部分方案不能满足”部分就不存在了。
注意这里计算的数据都挺大的,及时取模,小心溢出。

代码

// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MO = 1e9 + 7;

LL C[20][20];

inline void calc() {
    for(int i = 0; i <= 12; i++) {
        C[i][0] = 1;
        for(int j = 1; j <= i; j++) {
            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MO;
        }
    }
}

inline LL fpow(LL x, LL k) {
    LL t = 1;
    while(k) {
        if(k & 1) t = (t * x) % MO;
        x = (x * x) % MO;
        k >>= 1;
    }
    return t;
}

int T, n, k, l, cnt[20];
LL ans;

inline LL cal(int x) {
    if(!cnt[x]) return 1;
    LL t1 = 1, t2 = 1;
    for(int i = 0; i < x; i++) {
        if(!cnt[i]) continue;
        if(x - i > l) return 0;
        t1 = t1 * fpow(l - (x - i) + 1, cnt[i]) % MO;
        t2 = t2 * fpow(l - (x - i), cnt[i]) % MO;
    }
    if(x == k + 1) return fpow(t1, cnt[x]);
    t1 -= t2; if(t1 < 0) t1 += MO;
    t1 = fpow(t1, cnt[x]);
    return t1;
}

inline void dfs(int x, LL res, int tot) {
    if(x == k) {
        for(int i = 1; i + tot <= n; i++) {
            LL nres = res * C[n - tot - 1][i - 1] % MO * fpow(l, C[i][2]) % MO * fpow(l, C[n - tot - i][2]) % MO;
            cnt[k] = i; cnt[k + 1] = n - tot - i;
            nres = nres * cal(k) % MO * cal(k + 1) % MO;
            ans = (ans + nres) % MO;
        }
        return;
    }
    for(int i = 0; i + tot < n; i++) {
        cnt[x] = i;
        LL nres = res * fpow(l, C[i][2]) % MO * C[n - tot - 1][i] % MO * cal(x) % MO;
        dfs(x + 1, nres, tot + i);
    }
}

int main() {
    calc();
    T = readint();
    while(T--) {
        n = readint(); k = readint(); l = readint();
        memset(cnt, 0, sizeof(cnt));
        cnt[0] = 1;
        ans = 0;
        dfs(1, 1, 1);
        printf("%lld\n", ans);
    }
    return 0;
}
[HDU6166]Senior Pan 题解

[HDU6166]Senior Pan 题解

题目地址:HDUOJ:Problem – 6166 题目描述 Senior P 

[HNOI2009]最小圈 题解

[HNOI2009]最小圈 题解

题目地址:洛谷:【P3199】[HNOI2009]最小圈 – 洛谷、BZOJ: 

[CF295B]Greg and Graph 题解

[CF295B]Greg and Graph 题解

题目地址:Codeforces:Problem – 295B – Codeforces、洛谷:【CF295B】Greg and Graph – 洛谷

题目描述

Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game:

  • The game consists of n steps.
  • On the i-th step Greg removes vertex number xi from the graph. As Greg removes a vertex, he also removes all the edges that go in and out of this vertex.
  • Before executing each step, Greg wants to know the sum of lengths of the shortest paths between all pairs of the remaining vertices. The shortest path can go through any remaining vertex. In other words, if we assume that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex xi, then Greg wants to know the value of the following sum: \sum_{v, u, v \neq u} d(i, v, u).

Help Greg, print the value of the required sum before each step.
给一个完全图的邻接矩阵,按顺序删点,求每一次删点前剩下的点两两最短路长度的和。

输入输出格式

输入格式:
The first line contains integer n (1 ≤ n ≤ 500) — the number of vertices in the graph.
Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line aij (1 ≤ aij ≤ 105, aii = 0) represents the weight of the edge that goes from vertex i to vertex j.
The next line contains n distinct integers: x1, x2, …, xn (1 ≤ xi ≤ n) — the vertices that Greg deletes.

输出格式:
Print n integers — the i-th number equals the required sum before the i-th step.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.

输入输出样例

输入样例#1:

1
0
1

输出样例#1:

0

输入样例#2:

2
0 5
4 0
1 2

输出样例#2:

9 0

输入样例#3:

4
0 3 1 1
6 0 400 1
2 4 0 1
1 1 1 0
4 1 2 3

输出样例#3:

17 23 404 0 

题解

n的范围很小,用O(n^2)的枚举求和是可行的,但是删点这个就不怎么好办了。
正难则反,如果把删点改成加点是否可以呢?
多源最短路让我们想到了Floyd,其实Floyd的实质是一个DP,由于压过维,所以有点看不出,实际上这个式子可以写成这样
dp[k][i][j]= \min (dp[k-1][i][j], dp[k-1][i][k] + dp[k-1][k][j])
如果在这个意义下,我们发现第一维维k的时候指的是只有1~k的图中的最短路。我们考虑把给出的排列反着来跑,计算出x[k]~x[n]的图的最短路,再把这些点的最短路每个算一遍加起来,加进答案序列。

代码

// Code by KSkun, 2018/3
#include <cstdio>

#include <algorithm>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MAXN = 505;

int n, dis[MAXN][MAXN], x[MAXN];
LL ans[MAXN];

int main() {
    n = readint();
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            dis[i][j] = readint();
        }
    }
    for(int i = 1; i <= n; i++) {
        x[i] = readint();
    }
    for(int k = n; k >= 1; k--) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                dis[i][j] = std::min(dis[i][j], dis[i][x[k]] + dis[x[k]][j]);
            }
        }
        for(int i = n; i >= k; i--) {
            for(int j = n; j >= k; j--) {
                ans[k] += dis[x[i]][x[j]];
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        printf("%I64d ", ans[i]);
    }
    return 0;
}
[WF2012]Infiltration 题解

[WF2012]Infiltration 题解

题目地址:UVa:UVa Online Judge 题目描述 原题面参见UVa/vjudg