A simple problem - World at War

Background

This problem is from the book "Algorithm 4th edition" (Exersise 4.1.10)

There are N cities and M undirected roads between those cities. People can travel to any city along the roads.

One day, a war breaks out. Our cities are under attack! As we can't defend all …

more ...

How to implement a queue with stack(s)?

This problem is from the book Algorithms, 4th Edition.

Queue with three stacks. Implement a queue with three stacks so that each queue operation takes a constant (worst-case) number of stack operations.

Warning : high degree of difficulty.

When I search the Internet to find a solution, I find varieties of …

more ...

Codeforces Round #242 (Div. 2) Tutorials and Solutions

A. Squats

Trun x => X or X => x to make the number of 'x' is equal to the number of 'X'.

n = int(raw_input())
hamsters = [c for c in raw_input()]

sits = hamsters.count('x')
stands = hamsters.count('X')

if sits == stands:
    print 0
    print ''.join(hamsters)
else:
    if sits > stands …
more ...


Codeforces Round #223 (Div. 2) 不完全不正确题解

由于大号已经进Div. 1了,所以接下来的几场Div. 2都是用小号做的。

等有实力切D题了,再去打一区。(弱

事情一直很多,所以题解落后了好久才发。

A. Sereja and Dima

纯模拟,Python随便搞

n = int(raw_input())
pokers = map(int, raw_input().split())

v = [0, 0]
p = 0

for i in xrange(n):
    if pokers[0] > pokers[-1]:
        v[p] += pokers[0]
        del pokers[0]
    else:
        v[p] += pokers[-1 …
more ...

匈牙利算法

概念

交错路

交错路:设P是图G的一条路,如果P的任意两条相邻的边一定是一条属于M而另一条不属于M,就称P是一条交错路。

通俗点来说,就是把一个图中的路径染成红黑两种。然后找出一条路,使这条路红黑交错

注:交错路是无向图,图中的箭头只是为了便于观察。

例如下图中的:(3) -> (2) -> (1)

图1

从端点扩张交错路

假设我们已经有一个红黑交错路P,其端点为AB,其路径被标记为黑红。此时,我们向P中的一个端点(假设为A)加入一条边T

此时我们有边T + 交错路P(黑红)。当我们将边T标记为黑色时,我们就扩展了交错路P。 如果我们要保持红黑交错路的性质。则我们必须将边T …

more ...