Codeforces Round #286 (Div. 2) A B D

A - Mr. Kitayuta's Gift:插入一个字符判断能否组成回文串

<pre name="code" class="cpp">#include<iostream>
#include<stdio.h>
#include<string>

using namespace std;

string c = "qwertyuioplkjhgfdsazxcvbnm", cc, a;

bool judge()
{
    int r = cc.size();
    for( int k = 0 ; k < r ; k ++)
    {
        if(cc[k] != cc[r-k-1])
            return false;
    }
    return true;
}

int main()
{
    cin.sync_with_stdio(false);
    while( cin >> a )
    {
        int l = a.size();
        cc = "";
        for( int i = 0; i <= l; i++ )
        {
            for ( int j = 0; j < 26; j++ )
            {
                cc = "";
                for(int k = 0 ; k < i ; k ++)
                {
                    cc += a[k];
                }
                cc += c[j];
                for( int k = i ; k < l ; k ++)
                    cc += a[k];
                //cout << cc << endl;
                if( judge() )
                {
                    cout << cc << endl;
                    return 0;
                }
            }
        }
        cout << "NA" << endl;
    }
    return 0;
}


 
B - Mr. Kitayuta's Colorful Graph:给出一张图,边权为颜色值,q个询问,从起点到终点有多少种只经过一种颜色的就能到达终点的路径 

#include<stdio.h>
#include<string.h>
#include<algorithm>

using namespace std;

const int N = 150;

int a[N][N][N];

int main()
{
	int n, m, q;
	while(~scanf("%d%d", &n, &m))
	{
		int u, v, w;
		memset( a, 0, sizeof( a ));
		while(m--)
		{
			scanf("%d%d%d", &u, &v, &w);
			a[u][v][w] = 1;
			a[v][u][w] = 1;
		}
		for( int k = 1; k <= n; k++ )
		{
			for( int i = 1; i <= n; i++ )
			{
				for( int j = 1; j <= n; j++ )
				{
					for( int c = 1; c <= 100; c++ )
					{
						if( a[i][k][c] && a[k][j][c] )
						{
							a[i][j][c] = 1;
						}
					}
				}
			}
		}
		scanf("%d", &q);
		while( q-- )
		{
			int ans = 0;
			int u, w;
			scanf("%d%d", &u, &v);
			for( int i = 1; i <= 100; i++ )
				if( a[u][v][i] )
					ans++;
			printf("%d\n", ans);
		}
	}
	return 0;
}

D. Mr. Kitayuta's Technology:给出一张有向图,且题中按照给出的信息从起点到终点必须传递到,但是使添加的边数尽量小.比如题目第一组样例,1~4和1~3的边就不必添加,因为可以通过2传递到

思路:首先确定一个弱连通图里如果没有环,那么需要n-1条边,否则需要n条,所以只要tarjan缩点,然后并查集维护就行.

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int head[N];
int block[N];
int DFN[N];
int low[N];
int num[N];
bool mark[N];
int father[N];
bool instack[N];
bool vis[N];
stack <int> st;
int tot, sccnum, ord, n, m, cnt;

struct node
{
	int next;
	int to;
}edge[N << 1];

void init ()
{
	memset (head, -1, sizeof(head));
	memset (mark, 0, sizeof(mark));
	memset (father, -1, sizeof(father));
	memset (DFN, -1, sizeof(DFN));
	memset (num, 0, sizeof(num));
	memset (instack, 0, sizeof(instack));
	memset (vis, 0, sizeof(vis));
	tot = sccnum = ord = 0;
	while (!st.empty())
	{
		st.pop();
	}
}

int find (int x)
{
	if (father[x] == -1)
	{
		return x;
	}
	return father[x] = find (father[x]);
}

void addedge (int from, int to)
{
	edge[tot].to = to;
	edge[tot].next = head[from];
	head[from] = tot++;
}

void tarjan (int u)
{
	DFN[u] = low[u] = ++ord;
	instack[u] = 1;
	st.push (u);
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (DFN[v] == -1)
		{
			tarjan (v);
			low[u] = min (low[u], low[v]);
		}
		else if (instack[v])
		{
			low[u] = min (low[u], DFN[v]);
		}
	}
	if (low[u] == DFN[u])
	{
		int v;
		++sccnum;
		do
		{
			v = st.top();
			st.pop();
			instack[v] = 0;
			block[v] = sccnum;
			++num[sccnum];
		}while (v != u);
	}
}

void solve ()
{
	for (int i = 1; i <= n; ++i)
	{
		if (DFN[i] == -1)
		{
			tarjan (i);
		}
	}
	int ans = 0;
	for (int i = 1; i <= sccnum; ++i)
	{
		if (num[i] > 1)
		{
			mark[i] = 1;
		}
	}
	for (int u = 1; u <= n; ++u)
	{
		for (int i = head[u]; ~i; i = edge[i].next)
		{
			int v = edge[i].to;
			int fa = find (block[u]);
			int fb = find (block[v]);
			if (fa != fb)
			{
				father[fa] = fb;
				num[fb] += num[fa];
				mark[fb] |= mark[fa];
			}
		}
	}
	for (int i = 1; i <= sccnum; ++i)
	{
		if (father[i] == -1)
		{
			if (mark[i])
			{
				ans += num[i];
			}
			else
			{
				ans += num[i] - 1;
			}
		}
	}
	printf("%d\n", ans);
}

int main ()
{
	int u, v;
	while (~scanf("%d%d", &n, &m))
	{
		init();
		for (int i = 1; i <= m; ++i)
		{
			scanf("%d%d", &u, &v);
			addedge (u, v);
		}
		solve ();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值