HDU 4705 Y

Y

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 93    Accepted Submission(s): 32


Problem Description
 

Sample Input
  
4 1 2 1 3 1 4
 

Sample Output
  
1
Hint
1. The only set is {2,3,4}. 2. Please use #pragma comment(linker, "/STACK:16777216")
 

Source
 

Recommend
zhuyuanchen520
 
题意: 有一颗树, 选出3个点。 不在同一条路径上的集合数。
思路: DFS
间接法。
总的方案 - 在同一路径的。
从叶子节点开始。 
假设以i为节点子树的数量分别为 sum1, sum2, sumk;(i必选)
那么对每子树选一个点。 其他子树选一个点。有  sumi  * (n - 1 - sumi)
然后对i的子树选一个点,i树以外选一个点。
结果会重复算了一次。 所以要除上2.
#pragma comment(linker, "/STACK:16777216")
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int V = 100000 + 50;
const int mod = 1000000000 + 7;
vector<int> vec[V];
int n;
bool vis[V];
__int64 ans;
int dfs(int index) {
    int sum = 0;
    vis[index] = true;
    for(int i = 0; i < vec[index].size(); ++i)
        if(!vis[vec[index][i]]) {
            int temp = dfs(vec[index][i]);
            sum += temp;
            ans += (__int64) temp * (n - 1 - temp);
            //printf("%d %d %I64d\n", temp, sum, ans);
        }
    if(sum)
        ans += (__int64) sum * (n - 1 - sum);
    sum++;
    return sum;
}
int main() {
    int i, j;
    while(~scanf("%d", &n)) {
        ans = 0;
        memset(vis, false, sizeof(vis));
        for(i = 1; i < n; ++i) {
            int a, b;
            scanf("%d%d", &a, &b);
            vec[a].push_back(b);
            vec[b].push_back(a);
        }
        dfs(1);
        __int64 total = (__int64) n * (n - 1) * (n - 2) / 6;
        printf("%I64d\n", total - ans / 2);
        for(i = 1; i <= n; ++i)
            vec[i].clear();
    }
}

前向星链表
#pragma comment(linker, "/STACK:16777216")
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int V = 100000 + 50;
const int mod = 1000000000 + 7;
int pnt[V * 2], nxt[V * 2], e, head[V * 2];
int n;
bool vis[V];
__int64 ans;
inline void addedge(int u, int v) {
    pnt[e] = v;
    nxt[e] = head[u];
    head[u] = e++;
}
int dfs(int u) {
    int sum = 0;
    vis[u] = true;
    for(int i = head[u]; i != -1; i = nxt[i])
        if(!vis[pnt[i]]) {
            int temp = dfs(pnt[i]);
            sum += temp;
            ans += (__int64) temp * (n - 1 - temp);
            //printf("%d %d %I64d\n", temp, sum, ans);
        }
    if(sum)
        ans += (__int64) sum * (n - 1 - sum);
    sum++;
    return sum;
}
int main() {
    int i, j;
    while(~scanf("%d", &n)) {
        ans = e = 0;
        for(i = 0; i <= n; ++i) {
            vis[i] = false;
            head[i] = -1;
        }
        for(i = 1; i < n; ++i) {
            int a, b;
            scanf("%d%d", &a, &b);
            addedge(a, b);
            addedge(b, a);
        }
        dfs(1);
        __int64 total = (__int64) n * (n - 1) * (n - 2) / 6;
        printf("%I64d\n", total - ans / 2);
    }
}



给定的引用内容中未包含杭电OJ 1401的相关信息,因此无法直接依据引用给出该题目的解析及代码实现。 杭电OJ 1401的题目通常是关于“Solitaire”,其主要描述为在一个 8x8 的棋盘上,有 4 个棋子,每个棋子有一个初始位置和目标位置,需要判断是否能在 8 步之内将这 4 个棋子从初始位置移动到目标位置。 对于该题的解析思路: - 可以使用双向广度优先搜索(BFS),因为从起点和终点同时开始搜索能大大减少搜索空间和时间复杂度。 - 要对棋盘状态进行有效的存储和判断,可将 4 个棋子的位置信息组合成一个状态,使用哈希表来存储已访问的状态。 以下是一个可能的Python代码实现示例: ```python from collections import deque # 定义方向数组 dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] # 检查位置是否合法 def is_valid(x, y): return 0 <= x < 8 and 0 <= y < 8 # 生成状态的哈希值 def hash_state(state): state.sort() return tuple(state) # 双向BFS def bfs(start, end): queue1 = deque([(start, 0)]) queue2 = deque([(end, 0)]) visited1 = {hash_state(start): 0} visited2 = {hash_state(end): 0} while queue1 and queue2: # 从起点扩展 state1, step1 = queue1.popleft() if hash_state(state1) in visited2: total_step = step1 + visited2[hash_state(state1)] if total_step <= 8: return True for i in range(4): for j in range(4): x, y = state1[j] for k in range(4): new_x = x + dx[k] new_y = y + dy[k] while is_valid(new_x, new_y) and (new_x, new_y) not in state1: new_state = state1[:j] + [(new_x, new_y)] + state1[j + 1:] new_hash = hash_state(new_state) if new_hash not in visited1 and step1 + 1 <= 4: visited1[new_hash] = step1 + 1 queue1.append((new_state, step1 + 1)) new_x += dx[k] new_y += dy[k] # 从终点扩展 state2, step2 = queue2.popleft() if hash_state(state2) in visited1: total_step = step2 + visited1[hash_state(state2)] if total_step <= 8: return True for i in range(4): for j in range(4): x, y = state2[j] for k in range(4): new_x = x + dx[k] new_y = y + dy[k] while is_valid(new_x, new_y) and (new_x, new_y) not in state2: new_state = state2[:j] + [(new_x, new_y)] + state2[j + 1:] new_hash = hash_state(new_state) if new_hash not in visited2 and step2 + 1 <= 4: visited2[new_hash] = step2 + 1 queue2.append((new_state, step2 + 1)) new_x += dx[k] new_y += dy[k] return False while True: try: start = [] end = [] for _ in range(4): x, y = map(int, input().split()) start.append((x - 1, y - 1)) for _ in range(4): x, y = map(int, input().split()) end.append((x - 1, y - 1)) if bfs(start, end): print("YES") else: print("NO") except EOFError: break ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值