网络流 -- Alice's Chance(二分图多重最大匹配/最大流)

本文介绍了一个基于二分图多重最大匹配的问题,旨在解决一个演员如何能在限定时间内完成多个电影的拍摄任务。通过构造一个特殊的网络流模型并求解最大流,来确定演员是否能按照要求完成所有电影的拍摄。

解题报告

题目传送门

题意:

N个电影,每个电影在每一周有固定拍映时间,电影必须在W周前演完。有一个演员,他每天只能演一场电影,对于每部电影必须演完D天才算完。

思路:

二分图多重最大匹配问题,对于每个电影,源点与每个电影连上一条边容量为D,电影与每周7天对应拍映连线,容量为1,每周每天与汇点连线容量为1

在二分图最大匹配中,每个点(不管是X方点还是Y方点)最多只能和一条匹配边相关联,然而,我们经常遇到这种问题,即二分图匹配中一个点可以和多条匹配边相关联,但有上限,或者说,L i 表示点i最多可以和多少条匹配边相关联。

在原图上建立源点S和汇点T,S向每个X方点连一条容量为该X方点L值的边,每个Y方点向T连一条容量为该Y方点L值的边,原来二分图中各边在新的网络中仍存在,容量为1(若该边可以使用多次则容量大于1),求该网络的最大流,就是该二分图多重最大匹配的值。

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 99999999
using namespace std;
int mmap[400][400],n,l[400],m;
int bfs()
{
	memset(l,-1,sizeof(l));
	queue<int>Q;
	Q.push(0);
	l[0]=0;
	while(!Q.empty()) {
		int u=Q.front();
		Q.pop();
		for(int i=0; i<=m; i++) {
			if(l[i]==-1&&mmap[u][i]) {
				l[i]=l[u]+1;
				Q.push(i);
			}
		}
	}
	if(l[m]>1)
		return 1;
	return 0;
}
int dfs(int x,int f)
{
	int a;
	if(x==m)return f;
	for(int i=0; i<=m; i++) {
		if(l[i]==l[x]+1&&mmap[x][i]&&(a=dfs(i,min(f,mmap[x][i])))) {
			mmap[x][i]-=a;
			mmap[i][x]+=a;
			return a;
		}
	}
	l[x]=-1;
	return 0;
}
int main()
{
	int t,_h[10],d,w;
	cin>>t;
	while(t--) {
		memset(mmap,0,sizeof(mmap));
		memset(_h,0,sizeof(_h));
		cin>>n;
		int mon=0;
		int sum=0;
		for(int i=1; i<=n; i++) {
			scanf("%d%d%d%d%d%d%d%d%d",&_h[1],&_h[2],&_h[3],&_h[4],&_h[5],&_h[6],&_h[7],&d,&w);
			mmap[0][i]=d;
			sum+=d;
			if(mon<w)
				mon=w;
			for(int j=0; j<w; j++) {
				for(int k=1; k<=7; k++) {
					if(_h[k])
						mmap[i][n+j*7+k]=1;
				}
			}
		}
		m=n+mon*7+1;
		for(int i=n+1; i<=n+mon*7; i++)
			mmap[i][m]=1;
		int ans=0,a;
		while(bfs())
			while(a=dfs(0,inf))
				ans+=a;
		if(sum==ans)
			printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

Alice's Chance

Time Limit:  1000MSMemory Limit:  10000K
Total Submissions:  5277Accepted:  2168

Description

Alice, a charming girl, have been dreaming of being a movie star for long. Her chances will come now, for several filmmaking companies invite her to play the chief role in their new films. Unfortunately, all these companies will start making the films at the same time, and the greedy Alice doesn't want to miss any of them!! You are asked to tell her whether she can act in all the films.

As for a film, 

  1. it will be made ONLY on some fixed days in a week, i.e., Alice can only work for the film on these days; 
  2. Alice should work for it at least for specified number of days; 
  3. the film MUST be finished before a prearranged deadline.

For example, assuming a film can be made only on Monday, Wednesday and Saturday; Alice should work for the film at least for 4 days; and it must be finished within 3 weeks. In this case she can work for the film on Monday of the first week, on Monday and Saturday of the second week, and on Monday of the third week. 

Notice that on a single day Alice can work on at most ONE film. 

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a single line containing an integer N (1 <= N <= 20), the number of films. Each of the following n lines is in the form of "F1 F2 F3 F4 F5 F6 F7 D W". Fi (1 <= i <= 7) is 1 or 0, representing whether the film can be made on the i-th day in a week (a week starts on Sunday): 1 means that the film can be made on this day, while 0 means the opposite. Both D (1 <= D <= 50) and W (1 <= W <= 50) are integers, and Alice should go to the film for D days and the film must be finished in W weeks.

Output

For each test case print a single line, 'Yes' if Alice can attend all the films, otherwise 'No'.

Sample Input

Sample Output

Yes
No

Hint

A proper schedule for the first test case:



date     Sun    Mon    Tue    Wed    Thu    Fri    Sat

week1          film1  film2  film1         film1

week2          film1  film2  film1         film1

week3          film1  film2  film1         film1

week4          film2  film2  film2

Source


<think> 我们面对的问题是:使用 `usermod -d /home/marketing/alice -m alice` 命令修改用户主目录时,系统提示“usermod:无改变”。这意味着命令执行后没有发生任何变更。下面我们来分析可能的原因和解决方法。 ### 原因分析 1. **目标路径与当前主目录相同** 如果用户 `alice` 当前的主目录已经是 `/home/marketing/alice`,则无需修改,因此返回“无改变”。 2. **目录路径格式问题** - 路径末尾多余的斜杠(如 `/home/marketing/alice/`)可能导致系统认为路径不一致。 - 符号链接(Symbolic Link)路径与实际路径不一致。 3. **权限不足** - 未使用 `sudo` 执行命令。 - 新目录的父目录(如 `/home/marketing`)无写入权限。 4. **用户状态限制** - 用户 `alice` 当前已登录,进程占用原主目录。 ### 解决步骤 #### ✅ 步骤1:确认当前主目录 执行以下命令检查用户 `alice` 的当前主目录: ```bash grep '^alice:' /etc/passwd | cut -d: -f6 ``` - 若输出正是 `/home/marketing/alice`,则说明无需修改。 #### ✅ 步骤2:标准化路径格式 确保命令中的路径与当前配置**完全一致**: - 移除末尾斜杠:使用 `/home/marketing/alice` 而非 `/home/marketing/alice/`。 - 检查符号链接:通过 `realpath` 命令解析实际路径: ```bash realpath /home/marketing/alice ``` #### ✅ 步骤3:验证目录权限 1. 检查新目录父路径(`/home/marketing`)的所有权和权限: ```bash ls -ld /home/marketing ``` - 需确保: - 当前用户(或 `root`)有写入权限(`drwxr-xr-x` 中的 `w`)。 - 目录属组无冲突。 2. 若目录不存在,需提前创建并授权: ```bash sudo mkdir -p /home/marketing sudo chown root:root /home/marketing # 确保父目录属主为root ``` #### ✅ 步骤4:强制退出用户进程 若用户 `alice` 处于登录状态,强制结束其所有进程: ```bash sudo pkill -u alice # 终止用户所有进程 sudo usermod -d /home/marketing/alice -m alice ``` #### ✅ 步骤5:使用完整路径执行 避免路径解析错误,显式指定绝对路径: ```bash sudo usermod -d /home/marketing/alice -m alice ``` --- ### 验证是否生效 ```bash # 查看配置 grep 'alice' /etc/passwd # 检查目录迁移 ls -l /home/marketing/alice # 确认新目录存在且文件已迁移 ``` --- ### 替代方案:手动修改 若仍失败,可手动操作: 1. 编辑 `/etc/passwd`: ```bash sudo nano /etc/passwd ``` - 修改 `alice` 行中第六字段为 `/home/marketing/alice`。 2. 迁移数据: ```bash sudo cp -rp /home/alice /home/marketing/ # 假设原目录为/home/alice sudo chown -R alice:alice /home/marketing/alice ``` --- ### 故障排查流程图 ```mermaid graph TD A[执行 usermod -d /new/path -m user] --> B{报错“无改变”?} B -->|是| C[检查当前主目录] C -->|相同| D[无需操作] C -->|不同| E[验证路径格式] E --> F[检查权限/目录存在性] F --> G[终止用户进程] G --> H[重试命令] H --> I[成功] H -->|失败| J[手动修改/etc/passwd并迁移数据] ``` §§相关问题§§ 1. 如何解决 `usermod` 命令执行时提示 "用户已存在" 的问题? 2. Linux 中如何安全删除用户的旧主目录? 3. 修改主目录后 SSH 登录提示 "未找到主目录" 如何修复? 4. 批量迁移用户主目录到新磁盘分区的正确步骤是什么? </think> ### 解决 `usermod:无改变` 错误的方法 当执行 `sudo usermod -d /home/marketing/alice -m alice` 提示 **"usermod:无改变"** 时,表明系统认为无需修改。以下是逐步解决方案: --- #### 1. **确认当前主目录状态** ```bash grep ^alice /etc/passwd | cut -d: -f6 ``` - **检查输出**: - 若输出已经是 `/home/marketing/alice`,说明修改已生效(可能是重复执行命令) - 若输出为其他路径(如 `/home/alice`),继续下一步 --- #### 2. **解决路径不一致问题(常见原因)** **问题分析**: 系统检测到目标路径与现有路径**指向同一物理位置**(如符号链接或挂载点相同) **检查路径物理位置**: ```bash # 检查原目录物理路径 realpath $(grep ^alice /etc/passwd | cut -d: -f6) # 检查新目录物理路径 realpath /home/marketing/alice ``` - **若输出相同**:说明路径实际指向同一位置(即使逻辑路径不同) - **解决方案**: - 删除旧目录符号链接:`sudo rm /home/alice`(假设原路径是 `/home/alice`) - 重新执行命令 --- #### 3. **验证目录所有权(权限问题)** ```bash ls -ld /home/marketing /home/marketing/alice ``` - **必要检查**: - 父目录 `/home/marketing` 必须有 **`x` 执行权限**(否则无法访问子目录) - 新目录 `/home/marketing/alice` 应属于 `alice` 用户(或不存在) - **修复命令**: ```bash sudo chmod o+x /home/marketing # 添加其他用户执行权限 sudo mkdir -p /home/marketing/alice # 确保目录存在 sudo chown alice:alice /home/marketing/alice ``` --- #### 4. **处理用户进程占用** ```bash pgrep -u alice # 检查用户活动进程 ``` - **若存在活动进程**: ```bash sudo pkill -u alice # 终止用户所有进程 sudo usermod -d /home/marketing/alice -m alice ``` --- #### 5. **强制迁移(跳过路径检测)** ```bash sudo usermod -d /home/marketing/alice alice # 先修改路径(不带 -m) sudo cp -a /home/alice/. /home/marketing/alice/ # 手动复制数据 sudo chown -R alice:alice /home/marketing/alice ``` --- #### 6. **验证修改结果** ```bash # 检查配置文件 grep ^alice /etc/passwd # 测试登录(切换用户) su - alice echo $HOME # 应输出 /home/marketing/alice ``` --- ### 🔍 关键注意事项 1. **路径标准化**: 使用 `realpath` 工具确保新旧路径的物理位置不同 2. **权限继承**: 父目录需至少有 `755` 权限(`drwxr-xr-x`) 3. **用户状态**: 修改时用户必须**退出登录**(否则可能损坏数据)[^1] 4. **SELinux 影响**: 若启用 SELinux,需修复上下文: ```bash sudo restorecon -Rv /home/marketing/alice ``` --- ### 典型故障场景 | 现象 | 原因 | 解决方案 | |---------------------|-----------------------|------------------------| | 路径看似不同但实际相同 | 符号链接指向同一位置 | 删除旧符号链接 | | 父目录无执行权限 | 无法访问子目录 | `chmod o+x /home/marketing` | | 用户进程占用目录 | `sshd` 或 `bash` 占用 | 终止用户进程 |
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值